Send email with cURL
Prerequisites
Before you start, you need:
- A mails.ai API key. A test key (
mk_test_…) runs everything on this page and sends no real email. curlon your machine.curl --versiontells you whether it is there.
Guide
Set your API key
Keep the key in an environment variable, so it stays out of your shell history and scripts.
export MAILS_API_KEY="mk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"Send an email
POST /v1/messagessends one message.fromnames the agent that sends it: an agent with that name is used if it exists, and created if it does not when the key may create agents (themanagescope) and your plan has room for another; withoutmanagethe send answers403 insufficient_scope.curl https://api.mails.ai/v1/messages \ -H "Authorization: Bearer $MAILS_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: welcome-jordan-1" \ -d '{ "from": "hello", "to": "reply@test.mails.ai", "subject": "Welcome aboard", "body_text": "Thanks for signing up. Reply any time and I will read it." }'The answer is the sent message, with its
id, itsthread_idand the address it was sent as infrom. TheIdempotency-Keyheader makes a retry safe: sending the same key again returns the first answer instead of sending twice.Read the reply
reply@test.mails.aianswers within about a second. The reply arrives as areply.receivedevent with aninjection_scorefrom 0 to 1. How the test address works.curl "https://api.mails.ai/v1/events?event_type=reply.received&limit=5" \ -H "Authorization: Bearer $MAILS_API_KEY"Answer in the same thread
Take
data.source_message_idfrom the event (it starts withrcv_) and reply to it. The reply goes to the person who wrote and stays in the same conversation.curl https://api.mails.ai/v1/messages/rcv_01JZX8K3M9Q4P7VN2YB6RTDC0E/reply \ -H "Authorization: Bearer $MAILS_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "body_text": "Glad to hear it. Anything else I can help with?" }'
code to branch on and a request_id to quote when you ask for help. Errors lists the codes.Next steps
Was this page helpful?