AgentMail

AgentMail

Build a court deadline monitor with AI agents using AgentMail

Build a court deadline monitor with AI agents using AgentMail

Jun 10, 20264 min readBy AgentMail Examples

Legal teams struggle to catch court deadline reminders in inbox noise and manually draft timely attorney confirmations. AgentMail provides an Email Inbox API that lets your AI agents programmatically monitor, search, and reply to deadline notifications with real email. Use AgentMail's search and reply endpoints to let autonomous agents filter deadline reminders and draft confirmation responses for attorney review. This workflow keeps humans in control: agents prepare drafts, attorneys approve, ensuring compliance with ABA Model Rule 1.1 on AI competency.

What this tutorial covers

  • Outcome: You will build a TypeScript agent that monitors an email inbox for court deadline reminders, extracts deadline data, and auto-drafts attorney confirmation emails via AgentMail's API.
  • Endpoints used: `GET /v0/inboxes/{inbox_id}/messages/search`, `GET /v0/inboxes/{inbox_id}/messages/{message_id}`, `POST /v0/inboxes/{inbox_id}/messages/{message_id}/reply`, `POST /v0/inboxes/{inbox_id}/drafts`
  • SDK methods: `searchMessages()`, `getMessage()`, `replyToMessage()`, `createDraft()`
  • Language: typescript
  • Auth: Bearer token (Authorization header)
  • Estimated implementation time: ~18 minutes

Step 1: Set up the AgentMail client and authenticate

Initialize the AgentMail SDK with your API credentials to enable inbox access for deadline monitoring.

Initialize SDK client

typescript
1// Initialize AgentMail client with API credentials
2const api_token = process.env.API_TOKEN;
3const inbox_id = process.env.INBOX_ID;
4
5if (!api_token) {
6  throw new Error("API_TOKEN environment variable is required");
7}
8
9// Authenticate and verify inbox access by fetching a test message
10const res = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/messages/search`, {
11  method: "GET",
12  headers: {
13    "Authorization": `${api_token}`,
14    "Content-Type": "application/json"
15  }
16});
17
18const data = await res.json();
19console.log("AgentMail client initialized and authenticated. Inbox access verified.");

The SDK client is ready to make authenticated requests to AgentMail endpoints for your legal inbox.

Step 2: Search inbox for court deadline reminder emails

Query AgentMail's search endpoint to find incoming deadline notices from courts or tracking services. Filter by sender domain and keywords like 'deadline' or 'notice' to isolate relevant messages.

Query deadline messages

typescript
1// using inbox_id and api_token from Section 1 — redeclare for section scope
2// inbox_id and api_token are already defined above
3
4const res2 = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/messages/search`, {
5  method: "GET",
6  headers: {
7    "Authorization": `${api_token}`,
8    "Content-Type": "application/json"
9  }
10});
11const data2 = await res2.json();
12
13const deadline_messages = data2.messages;
14console.log(`Found ${deadline_messages.length} messages in search results`);
15for (const msg of deadline_messages) {
16  console.log(`Message ID: ${msg.message_id}, Thread: ${msg.thread_id}, Labels: ${msg.labels}`);
17}

Response:

json
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();
9// Response shape for court deadline reminder emails search:
10const response = {
11  "count": 0,
12  "limit": 0,
13  "next_page_token": "string",
14  "messages": [
15    {
16      "inbox_id": "string",
17      "thread_id": "string",
18      "message_id": "string",
19      "labels": [],
20      "timestamp": "2026-01-01T00:00:00Z"
21    }
22  ]
23};

Step 3: Extract deadline data from the full message body

Retrieve the complete message content using AgentMail's get message endpoint to parse structured deadline dates.

Fetch full message details

typescript
1const inbox_id = "inbox_123";
2const message_id = "msg_456";
3
4const res = await fetch(`https://api.agentmail.to/v0/inboxes/${inbox_id}/messages/${message_id}`, {
5  method: "GET",
6  headers: {
7    "Authorization": `${process.env.API_TOKEN}`,
8    "Content-Type": "application/json"
9  }
10});
11const message_data = await res.json();
12
13const deadline_dates = extractDeadlineDates(message_data);
14console.log("Extracted deadline dates:", deadline_dates);

Response:

json
1const res = await fetch("https://api.agentmail.to/v0/inboxes/{inbox_id}/messages/{message_id}", {
2  method: "GET",
3  headers: {
4    "Authorization": `${process.env.API_TOKEN}`,
5    "Content-Type": "application/json"
6  }
7});
8const data = await res.json();
9const deadline_data = {
10  inbox_id: data.inbox_id,
11  message_id: data.message_id,
12  thread_id: data.thread_id,
13  timestamp: data.timestamp,
14  labels: data.labels
15};

Step 4: Draft attorney confirmation response via AgentMail

Use AgentMail's draft creation endpoint to compose a structured confirmation email that the attorney can review before sending. This follows best practice: the agent prepares the draft, the attorney retains decision authority and client communication control.

Create confirmation draft

typescript
1// Draft a confirmation response using the inbox_id from Section 1
2const res4 = await fetch("https://api.agentmail.to/v0/inboxes/{inbox_id}/drafts", {
3  method: "POST",
4  headers: {
5    "Authorization": `${process.env.API_TOKEN}`,
6    "Content-Type": "application/json"
7  },
8  body: JSON.stringify({ "labels": ["string"], "reply_to": ["string"], "to": ["string"], "cc": ["string"], "bcc": ["string"] })
9});
10const data4 = await res4.json();
11// Response shape: { "inbox_id": "string", "draft_id": "string", "client_id": "string", "labels": ["string"], "reply_to": ["string"] }
12const preliminary_draft_id = data4.draft_id;
13console.log("Draft created with ID:", preliminary_draft_id);

Response:

json
1{
2  "inbox_id": "string",
3  "draft_id": "string",
4  "client_id": "string",
5  "labels": ["string"],
6  "reply_to": ["string"],
7  "to": ["string"],
8  "cc": ["string"],
9  "bcc": ["string"],
10  "subject": "string"
11}

Step 5: Send reply after attorney approval using AgentMail

Once the attorney reviews and approves the draft, use AgentMail's reply endpoint to send the confirmation back to the court. This two-step process (draft then send) enforces attorney oversight and maintains client communication responsibility.

Send approved confirmation

typescript
1// Step 1: Create a draft for attorney review
2const draftRes = await fetch(`https://api.agentmail.to/v0/inboxes/{inbox_id}/drafts`, {
3  method: "POST",
4  headers: {
5    "Authorization": `${process.env.API_TOKEN}`,
6    "Content-Type": "application/json"
7  },
8  body: JSON.stringify({ "to": ["court@example.com"], "subject": "Court Confirmation", "reply_to": ["attorney@firm.com"] })
9});
10const draft = await draftRes.json();
11const draft_id = draft.draft_id;
12console.log("Attorney review draft ID:", draft_id);
13
14// Step 2: After attorney approval, send reply to the original court message
15// using message_id from Section 3
16const replyRes = await fetch(`https://api.agentmail.to/v0/inboxes/{inbox_id}/messages/{message_id}/reply`, {
17  method: "POST",
18  headers: {
19    "Authorization": `${process.env.API_TOKEN}`,
20    "Content-Type": "application/json"
21  },
22  body: JSON.stringify({ "to": "court@example.com", "reply_to": "attorney@firm.com" })
23});
24const reply = await replyRes.json();
25console.log("Confirmation sent. Message ID:", reply.message_id);

Response:

json
1const res = await fetch("https://api.agentmail.to/v0/inboxes/{inbox_id}/messages/{message_id}/reply", {
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 after attorney approval:
11{
12  "message_id": "string",
13  "thread_id": "string"
14}

Common pitfalls when using AgentMail

  • Don't let agents send client-facing emails without attorney approval. Always draft first, then require attorney review before the agent calls the reply endpoint. ABA guidance mandates that attorneys retain control over client communications.
  • Avoid asking agents to interpret law or provide legal advice. Constrain agents to deadline extraction, document organization, and draft preparation. Final interpretation of deadlines, filing strategy, and legal decisions must remain with the attorney.
  • Monitor deadline search precision to avoid alert fatigue. Test your search query keywords against your actual inbox. Over-broad queries will flood drafts and reduce attorney trust in the system; refine patterns iteratively.

Ready to automate deadline tracking in your legal practice? Start building with AgentMail and set up your first monitored inbox today.

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

AgentMailAgentMail

Actionable email strategies for agent developers

© 2026 AgentMail, Inc. All rights reserved.