API mocks
On this page
SendGrid-compatible email API
A mock of the SendGrid v3 Mail Send API. Your app sends transactional email with @sendgrid/mail (or any SendGrid client, or plain HTTP) exactly as it would through SendGrid, and every accepted message lands in one of your team's Testlie email inboxes as a real email, with the HTML preview, link and accessibility checks, the spam report and everything else the email sandbox does. Nothing is delivered to real people.
Testlie is not affiliated with, endorsed or sponsored by Twilio or SendGrid. "SendGrid" is used only to describe API compatibility.
Connect
Create a SendGrid-compatible mock under Systems → Messaging → Messaging APIs. Use its base URL in place of https://api.sendgrid.com (without /v3: clients add /v3/mail/send themselves) and its API key:
| Credential | Use |
|---|---|
| API key | Authorization: Bearer <key>, like SendGrid. It starts with SG., so the SDK does not warn about its format |
@sendgrid/mail
setApiKey() resets the client's host to https://api.sendgrid.com, so point the client at the mock after calling it:
import sgMail from "@sendgrid/mail";
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.client.setDefaultRequest("baseUrl", "https://testlie.com/mock/<id>");
const [response] = await sgMail.send({
to: "jane@example.com",
from: { email: "app@example.com", name: "App" },
subject: "Welcome",
html: "<p>Hi Jane</p>",
});
response.statusCode; // 202
response.headers["x-message-id"]; // the message idThe SDK's typings do not declare client. In TypeScript, cast it: (sgMail as unknown as { client: { setDefaultRequest(key: "baseUrl", value: string): void } }).client.setDefaultRequest("baseUrl", url). If you configure the host from an environment variable, set it only when the variable is present so production keeps SendGrid's default. The same applies after setDataResidency(), which also resets the host.
You can also give the SDK your own client: const client = new Client(); client.setApiKey(key); client.setDefaultRequest("baseUrl", url); sgMail.setClient(client); with Client from @sendgrid/client (a direct dependency of your app in that case).
In Python, pass the base URL as host: SendGridAPIClient(os.environ["SENDGRID_API_KEY"], host="https://testlie.com/mock/<id>").
Errors
A missing key answers HTTP 401 with SendGrid's error body; a wrong one too:
{ "errors": [{ "message": "The provided authorization grant is invalid, expired, or revoked", "field": null, "help": null }] }Invalid requests answer 400 with one error that names the field, e.g. personalizations.0.to.0.email, from.email, subject, content, content.0.value, reply_to, attachments.0.content or template_id. Duplicate addresses across to, cc and bcc of one personalization are refused as SendGrid refuses them. A body over the mock's request size limit answers 413, the team's monthly email quota or inbox rate 429.
Where messages go
Each accepted message is captured in the mock's capture inbox (Settings tab). By default that is the oldest inbox of the mock's project, or else the team's oldest inbox; sends fail with HTTP 500 while the team has no inbox. Only your own team's inboxes can be chosen.
Captured messages count against the plan's monthly email messages and the inbox's per-minute rate, like mail sent over SMTP. Inbox webhooks, forwarding rules and automatic spam checks apply as usual.
Like SendGrid, each personalization is one message: its to and cc recipients are in the headers, bcc recipients are on the envelope only. Each message carries X-Message-Id (the value in the API response) and Message-ID: <X-Message-Id.N@sendgrid.testlie.invalid>, where N is the personalization's index, so tests can match the API response to the email.
Sandbox mode
With mail_settings.sandbox_mode.enable: true the request is validated as usual and answered with HTTP 200 and an empty body; nothing is captured and no events are sent, as with SendGrid. Without it, an accepted send answers 202 with an empty body and the X-Message-Id header.
Supported calls
| Call | Notes |
|---|---|
POST /v3/mail/send |
personalizations[] (to, cc, bcc, subject, headers, substitutions, dynamic_template_data, custom_args, from), from, reply_to, reply_to_list, subject, content[] (text/plain first, then text/html, then other types), attachments[] (base64 content, filename, type, disposition, content_id for inline images), template_id, headers, categories, custom_args, mail_settings.sandbox_mode. Up to 100 personalizations and 100 recipients per request (SendGrid allows 1,000) and 20 attachments |
GET /v3/scopes |
The key's scopes, including mail.send: a cheap key check |
GET /v3/user/profile |
A fixed test profile |
GET /v3/user/webhooks/event/settings |
Whether the Event Webhook is on (a webhook URL is set on the mock) and its URL |
GET /v3/user/webhooks/event/settings/signed |
{ "enabled": true, "public_key": "<base64>" }: the key that verifies Event Webhook signatures |
Templates: the mock has no template store. A send with template_id (and no content) is accepted and captured with a body that shows the template id and the personalization's dynamic_template_data, and an X-Testlie-Template-Id header. The subject is the personalization's subject, else dynamic_template_data.subject. Every request, with its template id, dynamic data, categories and custom args, is in the mock's call log.
Substitutions: legacy substitutions keys are replaced as plain text in the subject and content.
Accepted and ignored: send_at (messages are captured at once), batch_id, asm, ip_pool_name, tracking_settings and other mail settings. Not implemented: the Marketing, Contacts, Suppressions, Templates and Email Activity APIs.
Simulated outcomes
The recipient's address decides what happens: the local part itself or one of its +tags, e.g. bounce@example.com or jane+bounce@example.com. Dropped recipients are still accepted by the API (202), as with SendGrid, but are not captured.
| Address tag | Captured | Events |
|---|---|---|
| (none) | yes | processed, then delivered |
reject |
no | processed, dropped (Bounced Address) |
spam |
no | processed, dropped (Spam Reporting Address) |
unsub |
no | processed, dropped (Unsubscribed Address) |
bounce |
yes | processed, then bounce (type: "bounce") |
softbounce |
yes | processed, then bounce (type: "blocked") |
defer |
yes | processed, deferred, then delivered |
open |
yes | processed, then delivered and open |
click |
yes | processed, then delivered, open and click (on the first link of the HTML) |
Later events come after the mock's asynchronous steps delay (Scenarios tab, 2 seconds by default). The Outcome: decline scenario either drops every recipient (dropped) or refuses the send with HTTP 403 as SendGrid does for a from address that is not a verified Sender Identity (unverified_sender).
Event Webhook
Set a webhook URL on the mock to receive events. They arrive as SendGrid sends them: a JSON array of events in one POST, each with email, timestamp, event, smtp-id, sg_event_id, sg_message_id, category (when the send had categories) and your custom_args as top-level fields, plus reason, status, type, response, url and so on where the event has them. A batch can hold events for several recipients and several kinds; handle every element, not only the first.
sg_message_id is the X-Message-Id the send returned, followed by a dot and a per-recipient suffix (<X-Message-Id>.recvd-…), as with SendGrid. To find the message an event belongs to, compare the part before the first dot with the id you stored.
Every batch is signed like SendGrid's signed Event Webhook: X-Twilio-Email-Event-Webhook-Signature is the base64 ECDSA (P-256, SHA-256) signature of X-Twilio-Email-Event-Webhook-Timestamp followed by the raw body. Get the verification key from GET /v3/user/webhooks/event/settings/signed or the mock's snippets, and verify with @sendgrid/eventwebhook or Node's crypto:
import { createPublicKey, verify } from "node:crypto";
function verifySendGrid(publicKeyBase64, rawBody, signature, timestamp) {
const key = createPublicKey({ key: Buffer.from(publicKeyBase64, "base64"), format: "der", type: "spki" });
return verify("sha256", Buffer.from(timestamp + rawBody), key, Buffer.from(signature, "base64"));
}The webhook scenarios (delay, drop, duplicate, bad signature) apply, deliveries are retried, and each one is shown on the mock's Webhooks tab with a replay button.