> ## Documentation Index
> Fetch the complete documentation index at: https://docs.emailr.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Process Receiving Attachments

> Process attachments from receiving emails.

A common use case for Receiving emails is to process attachments.

<Info>
  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.
</Info>

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:

```js app/api/events/route.ts theme={null} theme={null}
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { Emailr } from 'emailr';

const emailr = new Emailr('em_xxxxxxxxx');

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

  if (event.type === 'email.received') {
    const { data: attachments } = await emailr
      .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](/docs/guides/dashboard/receiving/forward-emails).
