# Send an email with SendGrid

> Send an email from a workflow through your own SendGrid account, with an API connection and an HTTP request block.

To send an email from a workflow through your own SendGrid account, save a SendGrid API key as an [API connection](https://outwin.ai/docs/build/connect-an-api/) and call SendGrid's mail send endpoint from an [HTTP request](https://outwin.ai/docs/reference/blocks/http-request/) block. Every run that reaches the block sends one email from an address you have verified with SendGrid.

You need a SendGrid account, the member role or higher in the OutWin workspace, and edit access to the workflow's project. The SendGrid steps happen in SendGrid's own console at `app.sendgrid.com`. The Twilio Console at `console.twilio.com` is a separate site and has no Sender Authentication page.

## Verify a sender in SendGrid

SendGrid sends mail only from an address or a domain you have verified.

1. In the SendGrid console, open **Settings**, then **Sender Authentication**.
2. Under **Single Sender Verification**, select **Verify a Single Sender**.
3. Fill in the **Create a Sender** panel and select **Create**.
4. Open the email SendGrid sends to that address and select the link in it.

A single sender is enough to start. For production mail, authenticate your whole domain under **Domain Authentication** on the same page. A sender address at a free provider such as `gmail.com` draws a "not recommended" warning. It still works, and its mail may land in spam.

## Create a SendGrid API key

1. In the SendGrid console, open **Settings**, then **API Keys**, and select **Create API Key**.
2. Enter a name for the key.
3. Choose **Custom Access** and give **Mail Send** full access. Leave every other permission off.
4. Select **Create & View** and copy the key.

SendGrid shows the key once. It starts with `SG.`.

## Add the SendGrid connection

1. In OutWin, open **Connections** and select **Add API connection**.
2. In **Name**, enter `SendGrid`.
3. In **Base URL**, enter `https://api.sendgrid.com`. For an EU-region SendGrid account, enter `https://api.eu.sendgrid.com`.
4. Under **Authentication**, keep **Bearer token** and paste the key into **Token**.
5. Select **Add connection**.

After you save, nobody can read the key back. It can only be replaced, and every role sees a masked hint in its place. Members can add and edit a connection, and only owners and admins can delete one. [Connect an API](https://outwin.ai/docs/build/connect-an-api/) covers the other fields and how to replace a key.

## Add the HTTP request block

1. In the workflow, add an **HTTP request** block. The block palette lists it in the **API** group.
2. Under **Connection**, choose **SendGrid**.
3. Under **Request**, keep `POST` and enter the path `/v3/mail/send`.
4. Under **Body**, choose **JSON** and enter this body:

   ```json
   {
     "personalizations": [{ "to": [{ "email": "{{input.email}}" }] }],
     "from": { "email": "you@example.com" },
     "subject": "Welcome, {{input.name}}",
     "content": [{ "type": "text/plain", "value": "Thanks for signing up on {{today}}." }]
   }
   ```

5. Replace `you@example.com` with the sender you verified. SendGrid rejects any other from address.

The card now reads `POST /v3/mail/send · SendGrid`.

Each expression in the body sits inside quotes, so its value is inserted as text and escaped for JSON. `{{input.email}}` and `{{input.name}}` read the `email` and `name` fields of what the block receives. Change them to match what the preceding block passes on. The [HTTP request reference](https://outwin.ai/docs/reference/blocks/http-request/#settings) lists the expressions a body can hold.

Do not add an `Authorization` header to the block. The connection adds it to every request, and a workflow whose block sets that header cannot be published.

## Publish and check the send

Every run that reaches the block sends a real email, so use a recipient address you own for the first run.

[Publish the workflow and start a run](https://outwin.ai/docs/run/publish-and-start/). When SendGrid accepts the message, it answers `202` with an empty body, and the block's line in the run log reads `SendGrid answered 202 · POST /v3/mail/send`. A `202` means SendGrid accepted the message. It does not mean the message was delivered. To see delivery, open the **Activity** page in the SendGrid console. A separate workflow can start from SendGrid's events, such as `delivered`, `bounce`, and `open`, through a **Webhook** block whose **Source** is **SendGrid**. Each event starts its own run. [Start a workflow from SendGrid events](https://outwin.ai/docs/build/connect-an-api/start-a-workflow-from-sendgrid-events/) has the steps, and [Webhook settings](https://outwin.ai/docs/reference/triggers-approvals-and-waits/#webhook-settings) lists the block's settings.

SendGrid returns an id for the message in the `x-message-id` response header. The block directly after reads it as `{{input.response.headers["x-message-id"]}}`. A block further along reads it as `{{steps["Send email"].response.headers["x-message-id"]}}`, where `Send email` is the name of the HTTP request block.

## If the send fails

When SendGrid answers with an error status, the run shows as **Errored**. The block's message reads `SendGrid answered 403.`, with the status SendGrid returned, and the inspector shows what SendGrid said.

| What you see | Cause | Fix |
| --- | --- | --- |
| `403` with "The from address does not match a verified Sender Identity." | The from address in the body is not the sender you verified, or the link in the verification email was never selected. | Set the from address to the verified sender, or finish the verification in SendGrid. |
| `401` | The key is wrong, was deleted in SendGrid, or lacks **Mail Send** access. | Create a key with **Mail Send** access and replace the token on the connection's page. You do not republish the workflow. |

A request that was sent is charged even when SendGrid answers with an error. [What it costs](https://outwin.ai/docs/reference/blocks/http-request/#what-it-costs) says which failures add no charge. OutWin does not retry the request, so after you fix the cause, start a new run.
