sendgrid / sendgrid-nodejs

The Official Twilio SendGrid Led, Community Driven Node.js API Library
https://sendgrid.com
MIT License
2.98k stars 781 forks source link

No clear usage path for multiple keys #649

Closed tflanagan closed 6 years ago

tflanagan commented 6 years ago

Hi,

While I love SendGrid as a service, I'm starting to despise SendGrid's Nodejs implementation. It works great for a single API Key, but I'm venturing into the domain of requiring support for multiple API Keys.

Your repo is very confusing. Installing npm install sendgrid, brings us to this repository, which has an issue that was resolved (#275) that somehow gives us this ability, but every example I find is using @sendgrid/email. There isn't a clear path for me to provide my own client to the email instance, installing this repo didn't install the email class either.

Thanks for any help.

tflanagan commented 6 years ago

Nevermind, just consuming the API directly.

thinkingserious commented 6 years ago

Hi @tflanagan,

We are very interested in learning how we can improve this experience and I apologize for the frustration.

The current SDK for sending email has it's documentation here. Maybe part of the confusion is that this was a major community driven, led by @adamreisnz, update that was a breaking change that vastly improved the previous version.

To help get to the root of the issue, could you describe what trouble you ran into when trying to use multiple API Keys?

With Best Regards,

Elmer

adamreisnz commented 6 years ago

@tflanagan sorry for the confusion, please use the latest version of the @sendgrid/mail package, which has been released a few months ago to address some of the shortcomings of the previous NodeJS SDK.

We've had mostly positive feedback about the new implementation, but if there's anything you find off or encounter any problems please let us know.

As for using two API keys, if you are using the mailer in an Express environment or similar, you can set the API key you require in your middleware using:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

This way you can set the appropriate API key for each request as needed. If you have another use case where this approach doesn't work please let us know.

tflanagan commented 6 years ago

From my understanding, when you use setApiKey, you are setting a global, not instance specific value. So if I had two requests sent at the same time with each handler requiring a different key, this would fail.

adamreisnz commented 6 years ago

Yes that is correct, at this stage the client instance is created and exported basically as a singleton. I will look into exposing the class itself so that you can manually instantiate client instances with different API keys, shouldn't be a large effort.

thinkingserious commented 6 years ago

Thanks for taking the time to jump in @adamreisnz!

Thanks for the further clarification @tflanagan.

I will re-open this issue so that it will appear on our backlog for implementation.

adamreisnz commented 6 years ago

Have created a PR to address this, happy to get your feedback.