Loading...
Loading...
Compare original and translation side by side
extracted_textextracted_textundefinedundefined
Get your API key from https://console.agentmail.to/ or via the Agent sign-up API (see below).
**Python:**
```python
from agentmail import AgentMail
client = AgentMail(api_key="YOUR_API_KEY")
从https://console.agentmail.to/ 或通过Agent注册API获取您的API密钥(见下文)。
**Python:**
```python
from agentmail import AgentMail
client = AgentMail(api_key="YOUR_API_KEY")
**TypeScript:**
```typescript
import { AgentMailClient } from "agentmail";
const client = new AgentMailClient({ apiKey: "YOUR_API_KEY" });
**TypeScript:**
```typescript
import { AgentMailClient } from "agentmail";
const client = new AgentMailClient({ apiKey: "YOUR_API_KEY" });Requires(Python) /agentmail>=0.4.15(TypeScript). If your installed SDK raisesagentmail>=0.x, upgrade first.AttributeError: 'AgentMail' object has no attribute 'agent'
client = AgentMail() # no api_key needed for sign-up
response = client.agent.sign_up(
human_email="you@example.com",
username="my-agent",
)要求(Python版)/agentmail>=0.4.15(TypeScript版)。若您安装的SDK提示agentmail>=0.x,请先升级。AttributeError: 'AgentMail' object has no attribute 'agent'
client = AgentMail() # 注册无需传入api_key
response = client.agent.sign_up(
human_email="you@example.com",
username="my-agent",
)
```typescript
const client = new AgentMailClient();
const response = await client.agent.signUp({
humanEmail: "you@example.com",
username: "my-agent",
});
// response.apiKey, response.inboxId, response.organizationId
const authedClient = new AgentMailClient({ apiKey: response.apiKey });
await authedClient.agent.verify({ otpCode: "123456" });
```typescript
const client = new AgentMailClient();
const response = await client.agent.signUp({
humanEmail: "you@example.com",
username: "my-agent",
});
// response.apiKey, response.inboxId, response.organizationId
const authedClient = new AgentMailClient({ apiKey: response.apiKey });
await authedClient.agent.verify({ otpCode: "123456" });@agentmail.tofrom agentmail.inboxes.types import CreateInboxRequest@agentmail.tofrom agentmail.inboxes.types import CreateInboxRequest
```typescript
const inbox = await client.inboxes.create({
username: "support",
domain: "yourdomain.com",
displayName: "Support Agent",
clientId: "support-v1",
});
const inboxes = await client.inboxes.list();
const fetched = await client.inboxes.get("support@agentmail.to");
await client.inboxes.update("support@agentmail.to", { displayName: "New Name" });
await client.inboxes.delete("support@agentmail.to");@agentmail.to
```typescript
const inbox = await client.inboxes.create({
username: "support",
domain: "yourdomain.com",
displayName: "Support Agent",
clientId: "support-v1",
});
const inboxes = await client.inboxes.list();
const fetched = await client.inboxes.get("support@agentmail.to");
await client.inboxes.update("support@agentmail.to", { displayName: "New Name" });
await client.inboxes.delete("support@agentmail.to");@agentmail.totexthtmlsent = client.inboxes.messages.send(
inbox_id="agent@agentmail.to",
to="recipient@example.com", # string or list
subject="Hello from AgentMail",
text="Plain text body",
html="<p>HTML body</p>", # optional but recommended
cc="cc@example.com", # optional, string or list
bcc="bcc@example.com", # optional, string or list
reply_to="replies@example.com", # optional
labels=["outreach"], # optional
attachments=[{ # optional
"filename": "report.pdf",
"content": base64_content, # Base64-encoded
"content_type": "application/pdf",
}],
)texthtmlsent = client.inboxes.messages.send(
inbox_id="agent@agentmail.to",
to="recipient@example.com", # 字符串或列表
subject="Hello from AgentMail",
text="Plain text body",
html="<p>HTML body</p>", # 可选但推荐提供
cc="cc@example.com", # 可选,字符串或列表
bcc="bcc@example.com", # 可选,字符串或列表
reply_to="replies@example.com", # 可选
labels=["outreach"], # 可选
attachments=[{ # 可选
"filename": "report.pdf",
"content": base64_content, # Base64编码内容
"content_type": "application/pdf",
}],
)
```typescript
const sent = await client.inboxes.messages.send("agent@agentmail.to", {
to: "recipient@example.com",
subject: "Hello from AgentMail",
text: "Plain text body",
html: "<p>HTML body</p>",
cc: "cc@example.com",
labels: ["outreach"],
attachments: [{
filename: "report.pdf",
content: base64Content,
contentType: "application/pdf",
}],
});
```typescript
const sent = await client.inboxes.messages.send("agent@agentmail.to", {
to: "recipient@example.com",
subject: "Hello from AgentMail",
text: "Plain text body",
html: "<p>HTML body</p>",
cc: "cc@example.com",
labels: ["outreach"],
attachments: [{
filename: "report.pdf",
content: base64Content,
contentType: "application/pdf",
}],
});undefinedundefined
```typescript
const response = await client.inboxes.messages.list("agent@agentmail.to", {
limit: 10,
labels: ["unread"],
});
const msg = await client.inboxes.messages.get(
"agent@agentmail.to",
"<abc123@agentmail.to>",
);extracted_textextracted_htmltexthtmlhtmltext
```typescript
const response = await client.inboxes.messages.list("agent@agentmail.to", {
limit: 10,
labels: ["unread"],
});
const msg = await client.inboxes.messages.get(
"agent@agentmail.to",
"<abc123@agentmail.to>",
);extracted_textextracted_htmltexthtmlhtmltextreply = client.inboxes.messages.reply(
inbox_id="agent@agentmail.to",
message_id="<abc123@agentmail.to>",
text="Thanks for your email!",
html="<p>Thanks for your email!</p>", # optional
attachments=[...], # optional
reply_all=False, # optional, defaults to False
)const reply = await client.inboxes.messages.reply(
"agent@agentmail.to",
"<abc123@agentmail.to>",
{ text: "Thanks for your email!" },
);reply = client.inboxes.messages.reply(
inbox_id="agent@agentmail.to",
message_id="<abc123@agentmail.to>",
text="Thanks for your email!",
html="<p>Thanks for your email!</p>", # 可选
attachments=[...], # 可选
reply_all=False, # 可选,默认值为False
)const reply = await client.inboxes.messages.reply(
"agent@agentmail.to",
"<abc123@agentmail.to>",
{ text: "Thanks for your email!" },
);client.inboxes.messages.forward(
inbox_id="agent@agentmail.to",
message_id="<abc123@agentmail.to>",
to="colleague@example.com",
text="FYI, see below.", # optional prepended text
)await client.inboxes.messages.forward(
"agent@agentmail.to",
"<abc123@agentmail.to>",
{
to: "colleague@example.com",
text: "FYI, see below.",
},
);client.inboxes.messages.forward(
inbox_id="agent@agentmail.to",
message_id="<abc123@agentmail.to>",
to="colleague@example.com",
text="FYI, see below.", # 可选的前置文本
)await client.inboxes.messages.forward(
"agent@agentmail.to",
"<abc123@agentmail.to>",
{
to: "colleague@example.com",
text: "FYI, see below.",
},
);client.inboxes.messages.update(
inbox_id="agent@agentmail.to",
message_id="<abc123@agentmail.to>",
add_labels=["processed", "replied"],
remove_labels=["unread"],
)await client.inboxes.messages.update(
"agent@agentmail.to",
"<abc123@agentmail.to>",
{
addLabels: ["processed", "replied"],
removeLabels: ["unread"],
},
);client.inboxes.messages.update(
inbox_id="agent@agentmail.to",
message_id="<abc123@agentmail.to>",
add_labels=["processed", "replied"],
remove_labels=["unread"],
)await client.inboxes.messages.update(
"agent@agentmail.to",
"<abc123@agentmail.to>",
{
addLabels: ["processed", "replied"],
removeLabels: ["unread"],
},
);import base64import base64
```typescript
import { readFileSync } from "node:fs";
const content = readFileSync("report.pdf").toString("base64");
await client.inboxes.messages.send("agent@agentmail.to", {
to: "user@example.com",
subject: "Report attached",
text: "See attached.",
attachments: [{ filename: "report.pdf", content, contentType: "application/pdf" }],
});
const attachment = await client.inboxes.messages.getAttachment(
"agent@agentmail.to",
"<abc123@agentmail.to>",
"att_456",
);
```typescript
import { readFileSync } from "node:fs";
const content = readFileSync("report.pdf").toString("base64");
await client.inboxes.messages.send("agent@agentmail.to", {
to: "user@example.com",
subject: "Report attached",
text: "See attached.",
attachments: [{ filename: "report.pdf", content, contentType: "application/pdf" }],
});
const attachment = await client.inboxes.messages.getAttachment(
"agent@agentmail.to",
"<abc123@agentmail.to>",
"att_456",
);undefinedundefined
```typescript
const threads = await client.inboxes.threads.list("agent@agentmail.to", {
labels: ["unreplied"],
});
const thread = await client.inboxes.threads.get("agent@agentmail.to", "thd_123");
const allThreads = await client.threads.list();
```typescript
const threads = await client.inboxes.threads.list("agent@agentmail.to", {
labels: ["unreplied"],
});
const thread = await client.inboxes.threads.get("agent@agentmail.to", "thd_123");
const allThreads = await client.threads.list();undefinedundefined
```typescript
const draft = await client.inboxes.drafts.create("agent@agentmail.to", {
to: "recipient@example.com",
subject: "Pending approval",
text: "Draft content",
});
await client.inboxes.drafts.send("agent@agentmail.to", draft.draftId, {});
```typescript
const draft = await client.inboxes.drafts.create("agent@agentmail.to", {
to: "recipient@example.com",
subject: "Pending approval",
text: "Draft content",
});
await client.inboxes.drafts.send("agent@agentmail.to", draft.draftId, {});undefinedundefined
```typescript
const pod = await client.pods.create({ name: "customer-acme", clientId: "pod-acme-v1" });
const inbox = await client.pods.inboxes.create(pod.podId, {
username: "notifications",
clientId: "acme-notifications-v1",
});
const inboxes = await client.pods.inboxes.list(pod.podId);
```typescript
const pod = await client.pods.create({ name: "customer-acme", clientId: "pod-acme-v1" });
const inbox = await client.pods.inboxes.create(pod.podId, {
username: "notifications",
clientId: "acme-notifications-v1",
});
const inboxes = await client.pods.inboxes.list(pod.podId);(direction, type, entry).allow.blockdirection"send""receive""reply"type"allow""block"undefined(direction, type, entry).allow.blockdirection"send""receive""reply"type"allow""block"undefined
```typescript
await client.inboxes.lists.create(
"agent@agentmail.to",
"receive",
"allow",
{ entry: "boss@company.com" },
);
await client.inboxes.lists.create(
"agent@agentmail.to",
"receive",
"block",
{ entry: "spammer@example.com" },
);
const allow = await client.inboxes.lists.list(
"agent@agentmail.to",
"receive",
"allow",
);
await client.inboxes.lists.delete(
"agent@agentmail.to",
"receive",
"allow",
"boss@company.com",
);
```typescript
await client.inboxes.lists.create(
"agent@agentmail.to",
"receive",
"allow",
{ entry: "boss@company.com" },
);
await client.inboxes.lists.create(
"agent@agentmail.to",
"receive",
"block",
{ entry: "spammer@example.com" },
);
const allow = await client.inboxes.lists.list(
"agent@agentmail.to",
"receive",
"allow",
);
await client.inboxes.lists.delete(
"agent@agentmail.to",
"receive",
"allow",
"boss@company.com",
);agent@yourdomain.comundefinedagent@yourdomain.comundefined
```typescript
const domain = await client.domains.create({
domain: "yourdomain.com",
feedbackEnabled: true,
});
await client.domains.verify(domain.domainId);
```typescript
const domain = await client.domains.create({
domain: "yourdomain.com",
feedbackEnabled: true,
});
await client.domains.verify(domain.domainId);references/webhooks.mdreferences/websockets.mdreferences/webhooks.mdreferences/websockets.mdfrom agentmail import AgentMail, Subscribe, Subscribed, MessageReceivedEvent
client = AgentMail()
with client.websockets.connect() as socket:
socket.send_subscribe(Subscribe(inbox_ids=["agent@agentmail.to"]))
for event in socket:
if isinstance(event, Subscribed):
print(f"Subscribed to: {event.inbox_ids}")
elif isinstance(event, MessageReceivedEvent):
print(f"From: {event.message.from_}")
print(f"Subject: {event.message.subject}")
print(f"Body: {event.message.extracted_text}")from agentmail import AsyncAgentMail, Subscribe, MessageReceivedEvent
client = AsyncAgentMail()
async with client.websockets.connect() as socket:
await socket.send_subscribe(Subscribe(inbox_ids=["agent@agentmail.to"]))
async for event in socket:
if isinstance(event, MessageReceivedEvent):
await process_email(event.message)const socket = await client.websockets.connect();
socket.on("open", () => {
socket.sendSubscribe({ type: "subscribe", inboxIds: ["agent@agentmail.to"] });
});
socket.on("message", (event) => {
// Use event.eventType (not event.type — event.type is always "event")
if (event.eventType === "message.received") {
// TypeScript uses .from directly; only Python needs .from_ (reserved keyword)
console.log("From:", event.message.from);
console.log("Subject:", event.message.subject);
}
});from agentmail import AgentMail, Subscribe, Subscribed, MessageReceivedEvent
client = AgentMail()
with client.websockets.connect() as socket:
socket.send_subscribe(Subscribe(inbox_ids=["agent@agentmail.to"]))
for event in socket:
if isinstance(event, Subscribed):
print(f"已订阅: {event.inbox_ids}")
elif isinstance(event, MessageReceivedEvent):
print(f"发件人: {event.message.from_}")
print(f"主题: {event.message.subject}")
print(f"正文: {event.message.extracted_text}")from agentmail import AsyncAgentMail, Subscribe, MessageReceivedEvent
client = AsyncAgentMail()
async with client.websockets.connect() as socket:
await socket.send_subscribe(Subscribe(inbox_ids=["agent@agentmail.to"]))
async for event in socket:
if isinstance(event, MessageReceivedEvent):
await process_email(event.message)const socket = await client.websockets.connect();
socket.on("open", () => {
socket.sendSubscribe({ type: "subscribe", inboxIds: ["agent@agentmail.to"] });
});
socket.on("message", (event) => {
// 使用event.eventType(而非event.type —— event.type始终为"event")
if (event.eventType === "message.received") {
// TypeScript直接使用.from;仅Python需使用.from_(因from为保留关键字)
console.log("发件人:", event.message.from);
console.log("主题:", event.message.subject);
}
});event_typeswebhook = client.webhooks.create(
url="https://your-server.com/webhooks",
event_types=["message.received", "message.bounced"],
)event_typeswebhook = client.webhooks.create(
url="https://your-server.com/webhooks",
event_types=["message.received", "message.bounced"],
)
Typed webhook event types (listed in the SDK's Literal): `message.received`, `message.sent`, `message.delivered`, `message.bounced`, `message.complained`, `message.rejected`, `domain.verified`.
Runtime-only events — accepted by the API but not in the SDK's typed Literal — include `message.received.spam` and `message.received.blocked`. Pass them as plain strings if you need them. Type checkers will flag them; that's expected.
Always verify webhook signatures before processing. See `references/webhooks.md`.
已类型化的Webhook事件类型(SDK的Literal中列出):`message.received`, `message.sent`, `message.delivered`, `message.bounced`, `message.complained`, `message.rejected`, `domain.verified`。
仅运行时支持的事件——API接受但未包含在SDK的类型化Literal中——包括`message.received.spam`和`message.received.blocked`。如需使用请传入纯字符串,类型检查器会标记,此为预期行为。
处理前请始终验证Webhook签名,详见`references/webhooks.md`。client_idclientIdfrom agentmail.inboxes.types import CreateInboxRequest
inbox = client.inboxes.create(
request=CreateInboxRequest(client_id="my-unique-key"),
)client_idclientIdfrom agentmail.inboxes.types import CreateInboxRequest
inbox = client.inboxes.create(
request=CreateInboxRequest(client_id="my-unique-key"),
)undefinedundefinedRetry-Aftertry:
client.inboxes.messages.send(inbox_id, to="user@example.com", subject="Hi", text="Hello")
except Exception as e:
print(f"Error: {e}")
# e.body.message contains details if availableRetry-Aftertry:
client.inboxes.messages.send(inbox_id, to="user@example.com", subject="Hi", text="Hello")
except Exception as e:
print(f"错误: {e}")
# 若可用,e.body.message包含详细信息
```typescript
try {
await client.inboxes.messages.send(inboxId, {
to: "user@example.com",
subject: "Hi",
text: "Hello",
});
} catch (err) {
console.error("Error:", err.message);
// err.statusCode, err.body for details
}
// TypeScript: override retries globally on the client, or per-call via requestOptions
const client = new AgentMailClient({ apiKey: "...", maxRetries: 5 });
```typescript
try {
await client.inboxes.messages.send(inboxId, {
to: "user@example.com",
subject: "Hi",
text: "Hello",
});
} catch (err) {
console.error("错误:", err.message);
// err.statusCode, err.body包含详细信息
}
// TypeScript:在客户端全局覆盖重试次数,或通过requestOptions覆盖单次调用
const client = new AgentMailClient({ apiKey: "...", maxRetries: 5 });response = client.inboxes.messages.list(inbox_id, limit=20)
while response.next_page_token:
response = client.inboxes.messages.list(
inbox_id, limit=20, page_token=response.next_page_token
)response = client.inboxes.messages.list(inbox_id, limit=20)
while response.next_page_token:
response = client.inboxes.messages.list(
inbox_id, limit=20, page_token=response.next_page_token
)references/webhooks.mdreferences/websockets.mdreferences/full-api-reference.mdreferences/webhooks.mdreferences/websockets.mdreferences/full-api-reference.md