Skip to main content
A common use case for Inbound emails is to process attachments.
Webhooks do not include the actual content of attachments, only their metadata. You must call the Attachments API to retrieve the content. This design choice supports large attachments in serverless environments that have limited request body sizes.
Users can forward airplane tickets, receipts, and expenses to you. Then, you can extract key information from attachments and use that data. To do this, call the Attachments API after receiving the webhook event. That API will return a list of attachments with their metadata and a download_url that you can use to download the actual content. Note that the download_url is valid for 1 hour. After that, you will need to call the Attachments API again to get a new download_url. You can also check the expires_at field on each attachment to see exactly when it will expire. Here’s an example of getting attachment data in a Next.js application:
app/api/events/route.ts
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { Resend } from 'resend';

const resend = new Resend('re_xxxxxxxxx');

export const POST = async (request: NextRequest) => {
  const event = await request.json();

  if (event.type === 'email.received') {
    const { data: attachments } = await resend
      .attachments
      .receiving
      .list({ emailId: event.data.email_id });

    for (const attachment of attachments) {
      // use the download_url to download attachments however you want
      const response = await fetch(attachment.download_url);
      if (!response.ok) {
        console.error(`Failed to download ${attachment.filename}`);
        continue;
      }

      // get the file's contents
      const buffer = Buffer.from(await response.arrayBuffer());

      // process the content (e.g., save to storage, analyze, etc.)
    }

    return NextResponse.json({ attachmentsProcessed: attachments.length });
  }

  return NextResponse.json({});
};
Once you process attachments, you may want to forward the email to another address. Learn more about forwarding emails.