sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Enable sandbox mode for all requests

Open malimccalla opened this issue 5 years ago • 6 comments

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 avatar May 20 '20 15:05 malimccalla

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

malimccalla avatar Jun 01 '20 18:06 malimccalla

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 avatar Jun 02 '20 16:06 thinkingserious

Internal tracking#: CL-2681

thinkingserious avatar Jun 02 '20 23:06 thinkingserious

Is anyone working on this?

jasonribble avatar Sep 29 '21 20:09 jasonribble

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

mattiasjback avatar Nov 15 '21 14:11 mattiasjback

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.

astateful avatar Jan 04 '22 18:01 astateful