Integrations
Custom Agent
Connect Mailgent to your own agent via REST API
If your agent doesn't support MCP, use the REST API directly.
Authentication
Include your API key in every request:
Authorization: Bearer mgent-your-api-keyBase URL
https://api.mailgent.dev/v0Python Example
import requests
API_KEY = "mgent-your-api-key"
BASE = "https://api.mailgent.dev/v0"
headers = {"Authorization": f"Bearer {API_KEY}"}
# Send email
requests.post(f"{BASE}/messages/send", headers=headers, json={
"to": ["customer@example.com"],
"subject": "Hello",
"text": "This is from my Python agent."
})
# Check unread
response = requests.get(f"{BASE}/messages?labels=unread", headers=headers)
messages = response.json()["messages"]
for msg in messages:
print(f"From: {msg['from'][0]} — {msg['subject']}")
print(f"Text: {msg['extractedText']}")TypeScript Example
const API_KEY = "mgent-your-api-key";
const BASE = "https://api.mailgent.dev/v0";
const headers = {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
};
// Send email
await fetch(`${BASE}/messages/send`, {
method: "POST",
headers,
body: JSON.stringify({
to: ["customer@example.com"],
subject: "Hello",
text: "This is from my TypeScript agent.",
}),
});
// Check unread
const res = await fetch(`${BASE}/messages?labels=unread`, { headers });
const { messages } = await res.json();Agent Loop Pattern
import time
while True:
# 1. Check for unread
res = requests.get(f"{BASE}/messages?labels=unread", headers=headers)
messages = res.json()["messages"]
for msg in messages:
# 2. Get full thread for context
thread = requests.get(
f"{BASE}/threads/{msg['threadId']}", headers=headers
).json()
# 3. Generate reply with your LLM
reply_text = your_llm(thread["messages"])
# 4. Reply
requests.post(
f"{BASE}/messages/{msg['messageId']}/reply",
headers=headers,
json={"text": reply_text}
)
# 5. Mark as read
requests.patch(
f"{BASE}/messages/{msg['messageId']}",
headers=headers,
json={"addLabels": ["read", "processed"], "removeLabels": ["unread"]}
)
time.sleep(30) # Poll every 30 seconds