sendgrid / sendgrid-nodejs

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

Enable sandbox mode for all requests #1128

Open malimccalla opened 4 years ago

malimccalla commented 4 years ago

I have the following file that handles my sendgrid set up.

import sgMail from '@sendgrid/mail';

const sendGridApiKey = process.env.SENDGRID_API_KEY;

if (!sendGridApiKey) {
  throw new Error('The sendgrid api key has not been set in the environment variables');
}

sgMail.setApiKey(sendGridApiKey);

export { sgMail };

After setting my api key I need to be able to do something like this to enable sandbox mode on all requests

sgMail.enableSandbox(process.env.NODE_ENV !== 'production')

The absolute best solution would be not having to add a line of code like that at all but be able to generate an api key specifically for sandbox mode (similar to how Stripe has test keys). I really don't want to use my live production key in staging and development environments. Thanks!

malimccalla commented 4 years ago

I can see this was marked as non-library issue. Where is the correct place to open this issue?

thinkingserious commented 4 years ago

Hello @malimccalla,

We will route this request to the proper location internally on your behalf and update you here on any progress. Thank you!

With best regards,

Elmer

thinkingserious commented 4 years ago

Internal tracking#: CL-2681

jasonribble commented 3 years ago

Is anyone working on this?

mattiasjback commented 2 years ago

I want this feature as well. Any plans on implementing it soon?

astateful commented 2 years ago

In case anyone else runs across this, a solution could be to implement a server which mocks the appropriate sendgrid endpoints. The example setup for the mock endpoint:

sgClient.setApiKey(SOME_KEY);
sgClient.setDefaultRequest('baseUrl', 'MOCK_SERVER_ENDPOINT');
sgMail.setClient(sgClient);

The point being that the library will run all its validation logic (as it would in sandbox mode), and one can simply return dummy data/results from the mock server all while ignoring the api key (since the mock server doesnt have to validate the key).

But I am just doing this for unit tests (and rather simple use cases) and I therefore am not sure how it would scale to bigger dev/stage environments with more complex setups in which more complex and accurate server responses are required.