Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

cURL Examples for Common Use Cases



Exploring cURL Examples for Seamless SendGrid Integration

exploring-curl-examples-for-seamless-sendgrid-integration page anchor

To achieve effortless email integration with SendGrid through cURL, we've compiled a series of comprehensive examples that cover various use cases. Whether you're sending simple messages, multiple recipient emails, using templates, or scheduling emails, our cURL examples make the process a breeze.

Here's a basic cURL example to send a simple "Hello, World!" email through SendGrid's v3 Mail Send endpoint. Replace YOUR_API_KEY with your actual API key:



_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/send \
_10
--header 'Authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'


Sending a Basic Email to Multiple Recipients

sending-a-basic-email-to-multiple-recipients page anchor

For scenarios involving multiple recipients, our cURL example demonstrates how to send a basic email to multiple addresses:


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/send \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}],"cc": [{"email":"recipient2@example.com"}, {"email": "recipient3@example.com"}, {"email":"recipient4@example.com"}]}], "from": {"email": "sendeexampexample@example.com"},"subject":"Hello, World!", "content": [{"type": "text/plain", "value": "Heya!"}]}'


Sending a Basic Email Using a Template

sending-a-basic-email-using-a-template page anchor

If you prefer to use templates for your emails, this cURL example shows you how to send a basic email using a template. Replace YOUR_TEMPLATE_ID with your actual template ID:


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/send \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject":"Hello, World!","content": [{"type": "text/plain","value": "Heya!"}], "template_id" : "YOUR_TEMPLATE_ID"}'


Sending a Basic Email with Attachment

sending-a-basic-email-with-attachment page anchor

Our cURL example demonstrates how to send an email with attachments, catering to scenarios where you need to include files with your emails:


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/send \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sender@example.com"},"subject":"Hello, World!","content": [{"type": "text/html","value": "Hey, Please find attachment."}], "attachments": [{"content": "BASE64_ENCODED_CONTENT", "type": "text/plain", "filename": "attachment.txt"}]}'


Sending a Basic Email at a Scheduled Time

sending-a-basic-email-at-a-scheduled-time page anchor

In cases where scheduling emails is necessary, our cURL example demonstrates how to send a basic email at a specified time. Replace UNIX_TIMESTAMP_HERE with your desired timestamp:


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/send \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject":"Hello, World!","content": [{"type": "text/plain","value": "Heya!"}], "send_at" : UNIX_TIMESTAMP_HERE}'


Scheduling and Cancelling an Email

scheduling-and-cancelling-an-email page anchor

To explore advanced scheduling and email cancellation, we provide step-by-step guidance. You can schedule an email for future delivery and even cancel it if necessary.

You may schedule an email to be sent up to 72 hours in the future by using the send_at parameter. You may cancel this same scheduled email by using the Cancel Scheduled Sends endpoint.

Step 1: Generate a batch ID


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/batch \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \

Step 2: Schedule the email to be sent, using your new batch ID


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/mail/send \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject":"Hello, World!","content": [{"type": "text/plain","value": "Heya!"}], "send_at" : UNIX_TIMESTAMP_HERE, "batch_id" : "YOUR_BATCH_ID"}'

Step 3: Cancel the scheduled email


_10
curl --request POST \
_10
--url https://api.sendgrid.com/v3/user/scheduled_sends \
_10
--header 'authorization: Bearer YOUR_API_KEY' \
_10
--header 'Content-Type: application/json' \
_10
--data '{"batch_id":"YOUR_BATCH_ID","status":"cancel"}'


Rate this page: