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.
| Variable | Value |
|---|---|
baseUrl | https://api.sendgrid.com/v3 |
sendgridKey | SG.…, marked as secret |
fromEmail | a verified sender |
toEmail | a test inbox |
Authorization: Bearer token with {{sendgridKey}}.
Step 2: Send in sandbox mode
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}}
}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:
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
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
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}}}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');
});| Status | Cause |
|---|---|
400 | invalid body: bad address, missing content, unknown template |
401 | wrong or revoked key |
403 | the key lacks Mail Send, or the from address isn't a verified sender |
413 | the message, with attachments, is over 30 MB |
429 | rate limited |
The 403 for an unverified sender catches people on a new account: verify the sender first.