How to create a custom LangChain / LlamaIndex email tool for AI agents using AutoSend API?

We are building an autonomous AI customer agent with LangChain (Python/TypeScript). How do we define a structured Tool that enables the agent to draft and send transactional emails through AutoSend safely?

Building a LangChain AutoSend Email Tool

You can equip your LangChain AI agent with a strictly validated tool to send emails, using Zod / Pydantic schemas to ensure recipients and subjects are formatted correctly.

TypeScript / LangChain Example:

import { DynamicStructuredTool } from "@langchain/core/tools";
import { z } from "zod";

export const sendEmailTool = new DynamicStructuredTool({
  name: "send_transactional_email",
  description: "Send a transactional email notification or customer reply via AutoSend",
  schema: z.object({
    recipientEmail: z.string().email().describe("The recipient email address"),
    recipientName: z.string().optional().describe("The recipient full name"),
    subject: z.string().describe("The email subject line"),
    htmlBody: z.string().describe("The HTML formatted email message content"),
  }),
  func: async ({ recipientEmail, recipientName, subject, htmlBody }) => {
    const res = await fetch("https://api.autosend.com/v1/mails/send", {
      method: "POST",
      headers: {
        "Authorization": `Bearer ${process.env.AUTOSEND_API_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        from: { email: "assistant@yourdomain.com", name: "AI Support Agent" },
        to: { email: recipientEmail, name: recipientName },
        subject,
        html: htmlBody,
      }),
    });

    if (!res.ok) {
      const err = await res.text();
      return `Failed to send email: ${err}`;
    }

    const data = await res.json();
    return `Email successfully dispatched. Email ID: ${data.id}`;
  },
});

The LLM will automatically decide when an email notification is required, populate the arguments according to schema, and return the confirmation ID.

Relevant Documentation