Testing the SendGrid Mail Send API without emailing real people

SendGrid's Mail Send endpoint is simple: one POST, one JSON body. Testing it is the tricky part, because every real call sends a real email. SendGrid's sandbox mode solves that: the request is validated exactly as usual, and nothing is delivered.

Step 1: A restricted key

In SendGrid, Settings ▸ API Keys ▸ Create API Key ▸ Restricted Access, and give it only Mail Send. A full-access key in a test environment is a key that can delete your sender identities.

VariableValue
baseUrlhttps://api.sendgrid.com/v3
sendgridKeySG.…, marked as secret
fromEmaila verified sender
toEmaila test inbox

Authorization: Bearer token with {{sendgridKey}}.

Step 2: Send in sandbox mode

HTTP
POST {{baseUrl}}/mail/send
Content-Type: application/json

{
  "personalizations": [{"to": [{"email": "{{toEmail}}"}]}],
  "from": {"email": "{{fromEmail}}", "name": "API tests"},
  "subject": "Sandbox test",
  "content": [{"type": "text/plain", "value": "Hello from the API tests"}],
  "mail_settings": {"sandbox_mode": {"enable": true}}
}
JavaScript
pm.test('accepted by SendGrid', () => {
  pm.expect(pm.response.code).to.be.oneOf([200, 202]);
});

In sandbox mode SendGrid answers 200 and doesn't deliver; without it, 202 means queued. Neither has a body, so pm.response.json() would throw here. Test the status code, and the X-Message-Id header on real sends:

JavaScript
if (pm.response.code === 202) {
  pm.test('message id returned', () => pm.expect(pm.response.headers.get('X-Message-Id')).to.be.ok);
}

Step 3: Dynamic templates

HTTP
POST {{baseUrl}}/mail/send
Content-Type: application/json

{
  "from": {"email": "{{fromEmail}}"},
  "template_id": "{{welcomeTemplateId}}",
  "personalizations": [{
    "to": [{"email": "{{toEmail}}"}],
    "dynamic_template_data": {"firstName": "Ann", "plan": "Pro"}
  }],
  "mail_settings": {"sandbox_mode": {"enable": true}}
}

Sandbox mode checks that the template id exists and the request is valid. It doesn't render the template, so a missing {{firstName}} in the template itself won't fail here; preview those in SendGrid's template editor.

Step 4: The errors

HTTP
POST {{baseUrl}}/mail/send
Content-Type: application/json

{"personalizations": [{"to": [{"email": "not-an-email"}]}], "from": {"email": "{{fromEmail}}"}, "subject": "x", "content": [{"type": "text/plain", "value": "x"}], "mail_settings": {"sandbox_mode": {"enable": true}}}
JavaScript
pm.test('invalid address rejected', () => {
  pm.expect(pm.response.code).to.equal(400);
  const errors = pm.response.json().errors;
  pm.expect(errors).to.be.an('array');
  pm.expect(errors[0]).to.have.property('message');
});
StatusCause
400invalid body: bad address, missing content, unknown template
401wrong or revoked key
403the key lacks Mail Send, or the from address isn't a verified sender
413the message, with attachments, is over 30 MB
429rate limited

The 403 for an unverified sender catches people on a new account: verify the sender first.