Integration Guides · 6 MIN
How to send email with the Resend API from Python (and fix 403 error 1010)
Send transactional email through the Resend API with the Python standard library, stop duplicate sends with idempotency keys, and fix the 403 error code 1010.
To send email with the Resend API from Python, POST a JSON body with from, to, subject and html to https://api.resend.com/emails with your API key as a Bearer token. If you use Python's built-in urllib, also set your own User-Agent, or Resend answers 403 with error code: 1010. This guide covers the send, the 1010 fix, duplicate protection, and logging.
- Resend needs a verified sending domain, and the
fromaddress must use it. - Requests sent with urllib's default
Python-urllibUser-Agent were blocked with 403 error 1010 in our test on 17 November 2026. - The same request with a custom User-Agent, or with the
requests,httpxor Nodefetchdefaults, reached the API. - An
Idempotency-Keyheader stops the same email going out twice for 24 hours. - Always log the response body on an HTTP error, because the status code alone hides the cause.
- Nactore is a technology and growth company that builds, ships, ranks and grows products, and we send our own receipts and alerts this way.
What do you need before sending with Resend?
You need three things before the first Resend API call: an API key, a domain you have verified in the Resend dashboard, and a sender address on that domain. Without the verified domain, sends fail with a 403 that has nothing to do with your code.
| Item | Where it comes from | Note |
|---|---|---|
| API key | Resend dashboard, API Keys | Store in an environment variable |
| Verified domain | Resend dashboard, Domains | Add the DNS records it lists |
from address | Any mailbox on that domain | For example Orders <orders@yourdomain.com> |
Resend's domain guide walks through the DNS records.
Step 1: Send an email with the standard library
The send email endpoint needs only from, to and subject, plus a body. This version has no dependencies.
# mailer.py
import json, logging, os, urllib.error, urllib.request
log = logging.getLogger(__name__)
API = os.environ.get("RESEND_API_BASE", "https://api.resend.com")
def send_email(to, subject, html, idempotency_key=None):
headers = {
"Authorization": f"Bearer {os.environ['RESEND_API_KEY']}",
"Content-Type": "application/json",
"User-Agent": "yourapp-mailer/1.0", # urllib's default UA gets 403 error code 1010
}
if idempotency_key:
headers["Idempotency-Key"] = idempotency_key # same key within 24h = no second email
body = json.dumps({
"from": os.environ["MAIL_FROM"], # must be on a domain you verified in Resend
"to": [to],
"subject": subject,
"html": html,
}).encode()
req = urllib.request.Request(f"{API}/emails", data=body, headers=headers, method="POST")
try:
with urllib.request.urlopen(req, timeout=15) as resp:
return json.load(resp)["id"]
except urllib.error.HTTPError as err:
log.error("Resend HTTP %s: %s", err.code, err.read().decode(errors="replace"))
raiseemail_id = send_email(
"buyer@example.com", "Your receipt", "<p>Thanks for your order.</p>",
idempotency_key=f"receipt/{order.razorpay_order_id}",
)Why does Resend return 403 error code 1010?
Resend returns 403 with error code: 1010 when a request is blocked before it reaches the API, based on its User-Agent. Resend's 1010 article says every request must include a User-Agent header. The fix is one line: send your own, such as yourapp-mailer/1.0.
We hit this in production with a urllib client and first assumed the header was missing. So we measured it. On 17 November 2026 we sent an empty POST with no API key to the endpoint from each client below. A 401 means the request reached Resend and was refused only for the missing key, so no email could be sent.
| Client | User-Agent sent | Result |
|---|---|---|
Python urllib, default | Python-urllib/3.12 | 403, error code: 1010 |
Python urllib, custom | yourapp-mailer/1.0 | 401, reached the API |
Python urllib, empty | none | 401, reached the API |
requests 2.32.5 | python-requests/2.32.5 | 401, reached the API |
httpx 0.28.1 | python-httpx/0.28.1 | 401, reached the API |
Node.js fetch | Node default | 401, reached the API |
So in our test the block hit urllib's default string, not a missing header. That can change, which is why the safe rule is to always send your own User-Agent.
Step 2: Stop duplicate emails with an idempotency key
Webhooks retry and users double click, so the same receipt can be triggered twice. Resend's idempotency keys solve this. Repeat a key with the same payload within 24 hours and Resend returns the original response without sending again. Reuse a key with a different payload and you get a 409.
Resend suggests keys shaped like <event-type>/<entity-id>, which is why the example uses receipt/<order id>.
Tie the idempotency key to the business event, not to the HTTP attempt. A key built from the order ID survives retries, a key built from a random UUID per attempt does not.
Step 3: Log the error body in Django
The 1010 response looks like an auth failure unless you read the body, and mailer.py logs it on every HTTPError. In Django those log lines only appear if logging is configured. The Django logging docs explain that the default setup shows little once DEBUG is off, so set LOGGING explicitly.
# settings.py
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"root": {"handlers": ["console"], "level": "INFO"},
"loggers": {"django": {"handlers": ["console"], "level": "INFO", "propagate": False}},
}On hosts that collect stdout and stderr, these lines show up in the service logs.
What we tested
| Check | Result |
|---|---|
Send returns the email id | Pass, against a local mock |
| Bearer auth, custom User-Agent and body fields sent | Pass |
| Same idempotency key sends once | Pass, mock follows Resend's documented behavior |
A 401 is raised and its body logged through LOGGING | Pass |
| Default urllib User-Agent blocked | Reproduced against the live API, no key used |
The mock cannot prove deliverability or domain setup. Send one real email to yourself with a test key before launch.
Frequently asked questions
Do I need the Resend Python SDK?
No. One POST covers sending, and the standard library handles it. Resend's SDKs set a User-Agent for you, which avoids the 1010 error if you prefer them.
Why does Resend return 403 when my User-Agent is fine?
A 403 can also mean the from domain is not verified or does not match your account's domains. Read the response body to see which one.
How long does a Resend idempotency key last?
Resend stores keys for 24 hours. After that a repeated key is not checked against the earlier send.
Can I send to several recipients in one call?
Yes. The to field accepts up to 50 addresses. For separate emails per person, send one request each.
When to get help
If your transactional email silently fails or lands in spam, tell us what you are seeing or see what we build. For the payment flow that triggers these receipts, read how to integrate Razorpay in Django. Nactore is one partner that builds, ships, ranks and grows, measured on your growth.
Want to apply this to your business?
Book a free 30-minute call. We will tell you what we would do first.