Regulatory agencies send critical compliance notices to your inbox, but manual review and response drafting delays your firm's ability to stay audit-ready. AgentMail provides an Email Inbox API enabling AI agents to monitor incoming regulatory notices and auto-generate compliant response drafts in real time. AgentMail's search and message retrieval endpoints let your agent scan for regulatory notices, extract key details, and trigger automated compliance workflows. By combining inbox monitoring with draft creation, you can hand off AI-generated responses to human reviewers for final approval without losing time.
What this tutorial covers
- •Outcome: You can build an autonomous compliance agent that monitors regulatory email, extracts notice details, and auto-drafts audit-ready responses via AgentMail's REST API.
- •Endpoints used: `GET /v0/inboxes/{inbox_id}/messages/search`, `GET /v0/inboxes/{inbox_id}/messages/{message_id}`, `GET /v0/inboxes/{inbox_id}/messages/{message_id}/attachments/{attachment_id}`, `POST /v0/inboxes/{inbox_id}/drafts`, `PATCH /v0/inboxes/{inbox_id}/drafts/{draft_id}`, `POST /v0/inboxes/{inbox_id}/messages/{message_id}/reply`
- •Language: typescript
- •Auth: Bearer token (Authorization header)
- •Estimated implementation time: ~18 minutes
Step 1: Search for regulatory notices in your AgentMail inbox
Your compliance agent needs to detect incoming regulatory notices from known agencies. Use AgentMail's search endpoint to query for messages matching regulatory keywords and sender domains. This enables continuous monitoring without polling every message in your inbox.
Query inbox for regulatory alerts
1const inboxId = process.env.INBOX_ID;
2const regulatoryKeywords = "regulatory OR compliance OR notice OR SEC OR CFTC OR FCA";
3const regulatoryDomains = "@sec.gov @cftc.gov @fca.org.uk";
4const query = `${regulatoryKeywords} from:(${regulatoryDomains})`;
5
6const res = await fetch(`https://api.agentmail.to/v0/inboxes/${inboxId}/messages/search`, {
7 method: "GET",
8 headers: {
9 "Authorization": `${process.env.API_TOKEN}`,
10 "Content-Type": "application/json"
11 }
12});
13const data = await res.json();
14
15for (const message of data.messages) {
16 console.log(`Regulatory notice detected: ${message.message_id} at ${message.timestamp}`);
17}
18
19// Store first message for use in subsequent sections
20const firstMessage = data.messages[0];
21const detectedMessageId = firstMessage?.message_id;Response:
1const res = await fetch("https://api.agentmail.to/v0/inboxes/{inbox_id}/messages/search", {
2 method: "GET",
3 headers: {
4 "Authorization": `${process.env.API_TOKEN}`,
5 "Content-Type": "application/json"
6 }
7});
8const data = await res.json();
9const searchResponse = {
10 "count": 0,
11 "limit": 0,
12 "next_page_token": "string",
13 "messages": [
14 {
15 "inbox_id": "string",
16 "thread_id": "string",
17 "message_id": "string",
18 "labels": [],
19 "timestamp": "2026-01-01T00:00:00Z"
20 }
21 ]
22};Step 2: Retrieve full message and attachments for analysis
Once your agent identifies a regulatory notice, fetch the full message and any attached guidance documents. AgentMail's message and attachment endpoints provide complete notice details. Extract attachment content to feed into your compliance analysis model.
Fetch full notice with attachments
1async function retrieveRegulatoryNoticeDetails(inbox_id: string, message_id: string) {
2 const auth_header = `${process.env.API_TOKEN}`;
3
4 const message_res = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/messages/${message_id}`, {
5 method: "GET",
6 headers: {
7 "Authorization": auth_header,
8 "Content-Type": "application/json"
9 }
10 });
11 const message_data = await message_res.json();
12
13 const attachment_ids = ["attach_1", "attach_2"];
14 for (const attachment_id of attachment_ids) {
15 const attachment_res = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/messages/${message_id}/attachments/${attachment_id}`, {
16 method: "GET",
17 headers: {
18 "Authorization": auth_header,
19 "Content-Type": "application/json"
20 }
21 });
22 const attachment_data = await attachment_res.json();
23 console.log(`Attachment: ${attachment_data.filename}, Size: ${attachment_data.size}`);
24 }
25
26 return { message: message_data, inbox_id: message_data.inbox_id };
27}
28
29// Retrieve details using the inbox and message detected in Section 1
30const noticeDetails = await retrieveRegulatoryNoticeDetails(inboxId!, detectedMessageId!);
31console.log("Retrieved regulatory notice details for message:", detectedMessageId);Response:
1{
2 "inbox_id": "string",
3 "thread_id": "string",
4 "message_id": "string",
5 "labels": ["string"],
6 "timestamp": "2026-01-01T00:00:00Z",
7 "attachment_id": "string",
8 "filename": "string",
9 "size": 0,
10 "content_type": "string",
11 "content_disposition": "inline"
12}Step 3: Create a draft compliance response with AgentMail
Your AI agent has analyzed the regulatory notice and identified required actions. Use AgentMail's draft endpoint to create a compliant response email, then iterate with PATCH before sending. This gives human compliance officers a structured handoff point for final review and approval.
Generate compliance response draft
Response:
1const res = await fetch("https://api.agentmail.to/v0/inboxes/{inbox_id}/drafts", {
2 method: "POST",
3 headers: {
4 "Authorization": `${process.env.API_TOKEN}`,
5 "Content-Type": "application/json"
6 },
7 body: JSON.stringify({ "labels": ["string"], "reply_to": ["string"], "to": ["string"], "cc": ["string"], "bcc": ["string"] })
8});
9const data = await res.json();
10// Response shape for compliance draft:
11{
12 "inbox_id": "string",
13 "draft_id": "string",
14 "client_id": "string",
15 "labels": ["string"],
16 "reply_to": ["string"]
17}Step 4: Refine the draft with AgentMail before human review
Your compliance team flags that the draft needs stronger language around model explainability. Use AgentMail's PATCH endpoint to update the draft with revised content. Iterate the response body until it passes your firm's compliance standards, then route to an authorized officer for approval.
Update draft with strengthened language
1// Using inbox_id and draft_id from Section 3
2const inbox_id = "inbox-123";
3const draft_id = "draft-456";
4
5const res = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/drafts/${draft_id}`, {
6 method: "PATCH",
7 headers: {
8 "Authorization": `${process.env.API_TOKEN}`,
9 "Content-Type": "application/json"
10 },
11 body: JSON.stringify({ "subject": "Revised Policy Framework with Enhanced Model Explainability Requirements" })
12});
13const data = await res.json();
14
15if (data.draft_id) {
16 console.log("Draft updated successfully. Routing to authorized officer for approval.");
17} else {
18 throw new Error("Draft update failed to pass compliance standards.");
19}Response:
1const res = await fetch("https://api.agentmail.to/v0/inboxes/{inbox_id}/drafts/{draft_id}", {
2 method: "PATCH",
3 headers: {
4 "Authorization": `${process.env.API_TOKEN}`,
5 "Content-Type": "application/json"
6 },
7 body: JSON.stringify({ "reply_to": ["string"], "to": ["string"], "cc": ["string"], "bcc": ["string"], "subject": "string" })
8});
9const data = await res.json();
10// Response shape after refining draft with AgentMail:
11// {
12// "inbox_id": "string",
13// "draft_id": "string",
14// "client_id": "string",
15// "labels": ["string"],
16// "reply_to": ["string"]
17// }Step 5: Send the approved compliance response via AgentMail
After your compliance officer approves the draft, your agent sends the response as a formal email reply. AgentMail's reply endpoint ensures the message is threaded with the original regulatory notice. This creates an audit trail linking each regulatory notice to your firm's documented compliance commitment.
Send approved response as threaded reply
1// After compliance officer approval, send formal response via AgentMail reply
2const inbox_id = process.env.COMPLIANCE_INBOX_ID;
3const message_id = process.env.REGULATORY_NOTICE_MESSAGE_ID;
4
5const res = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/messages/${message_id}/reply`, {
6 method: "POST",
7 headers: {
8 "Authorization": `${process.env.API_TOKEN}`,
9 "Content-Type": "application/json"
10 },
11 body: JSON.stringify({
12 "labels": ["compliance-response", "audit-trail"],
13 "reply_to": process.env.COMPLIANCE_OFFICER_EMAIL,
14 "to": process.env.REGULATOR_EMAIL
15 })
16});
17const data = await res.json();
18console.log("Compliance response sent. Thread ID:", data.thread_id, "Message ID:", data.message_id);Response:
{
"message_id": "string",
"thread_id": "string"
}Common pitfalls when using AgentMail
- •Unvalidated AI responses create regulatory risk. Never send compliance responses directly from AI without human review. Modern AI compliance platforms emphasize audit-ready documentation and model oversight. Always route AI-generated drafts to a designated risk owner or policy officer for approval before transmission.
- •Missing attachment context breaks analysis. Regulatory notices often include multi-page guidance documents. Always retrieve and analyze attachments before drafting responses. Skipping attachment review may cause your compliance response to miss critical requirements embedded in referenced policies.
- •Search queries must be narrow and specific. Overly broad searches on domain names alone will catch marketing emails and false positives. Combine sender domain, keywords ('compliance', 'regulation', 'notice'), and date ranges to isolate true regulatory notices and reduce noise.
- •Thread continuity is critical for audit trails. Use the reply endpoint rather than creating standalone messages. Threaded replies create an auditable conversation chain linking the agency's notice to your firm's documented response, which regulators expect during examinations.
Ready to automate your compliance response workflow? Get started with AgentMail and build a regulatory monitoring agent that keeps your firm audit-ready.
Documentation references
The code examples in this tutorial are grounded in the following docs pages:
- •
Ready to give your agents real email access?
Join leading developers using AgentMail to enable AI agents to send, receive, and search email natively via API.
Read More Blog Posts
Monitor invoice replies and auto-draft escalation reminders
Manual review of invoice reply threads delays escalation, and drafting payment reminders requires consistent context and tone. AgentMail provides an Email Inbox
Auto-triage incident alerts and escalate with AgentMail
DevOps teams waste time manually sorting noisy alert floods and drafting escalation emails to on-call responders during incidents. AgentMail provides an Email I

