LangChain agents can use tools to interact with external services. Adding AgentMailr tools gives your LangChain application the ability to handle email verification flows autonomously.

Define Tools Using @tool Decorator

from langchain.tools import tool
import httpx

BASE = "https://api.agentmailr.com"
HEADERS = {"X-API-Key": "ak_your_key", "Content-Type": "application/json"}

@tool
def create_inbox(name: str = "") -> dict:
    """Create a new email inbox. Returns id and address."""
    r = httpx.post(f"{BASE}/v1/inboxes", headers=HEADERS, json={"name": name})
    return r.json()

@tool
def wait_for_otp(inbox_id: str, timeout: int = 60) -> str:
    """Wait for an OTP code to arrive in an inbox. Returns the code."""
    r = httpx.get(
        f"{BASE}/v1/inboxes/{inbox_id}/otp",
        headers=HEADERS,
        params={"timeout": timeout},
        timeout=timeout + 5
    )
    data = r.json()
    return data.get("code", "No OTP received")

@tool
def send_email(inbox_id: str, to: str, subject: str, text: str) -> dict:
    """Send an email from an inbox."""
    r = httpx.post(
        f"{BASE}/v1/inboxes/{inbox_id}/send",
        headers=HEADERS,
        json={"to": to, "subject": subject, "text": text}
    )
    return r.json()

Create an Agent with These Tools

from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate

llm = ChatAnthropic(model="claude-sonnet-4-6")
tools = [create_inbox, wait_for_otp, send_email]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an agent that can manage email inboxes and handle verification flows."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

result = executor.invoke({
    "input": "Create an inbox, then send a test email to demo@example.com from it."
})

Use in Chains

These tools work in any LangChain chain or agent: ReAct agents, OpenAI Functions agents, and LCEL chains with tool calling. The same tool definitions work with both Python and JavaScript LangChain.