usend Docs
Receiving

Receiving attachments

Download the attachments of emails you receive.

The email.received webhook lists each attachment's id, filename, content_type and size, but not its content. Download the content with the API.

Find the attachment IDs

The webhook payload and GET /inbound/emails/{id} both list the attachments of a received email:

Shell
curl https://api.usend.et/inbound/emails/a39999a6-88e3-48b1-888b-beaabcde1b33 \
  -H "Authorization: Bearer us_123456789"
JSON
{
  "object": "inbound_email",
  "id": "a39999a6-88e3-48b1-888b-beaabcde1b33",
  "...": "...",
  "attachments": [
    { "id": "2a0c9ce0-3112-4728-976e-47ddcd16a318", "filename": "screenshot.png", "content_type": "image/png", "size": 182044 }
  ]
}

Download an attachment

GET /inbound/emails/{id}/attachments/{attachment_id} returns the file itself. It needs your API key:

Shell
curl -o screenshot.png \
  https://api.usend.et/inbound/emails/a39999a6-88e3-48b1-888b-beaabcde1b33/attachments/2a0c9ce0-3112-4728-976e-47ddcd16a318 \
  -H "Authorization: Bearer us_123456789"

Example: save every attachment

Node.js
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { Usend } from '@usend/node';

const usend = new Usend(process.env.USEND_API_KEY);
const headers = { Authorization: `Bearer ${process.env.USEND_API_KEY}` };

export async function saveAttachments(emailId) {
  const { data: email, error } = await usend.received.get(emailId);
  if (error) throw new Error(error.message);
  for (const att of email.attachments) {
    const url = `https://api.usend.et/inbound/emails/${emailId}/attachments/${att.id}`;
    const file = await fetch(url, { headers });
    await writeFile(path.basename(att.filename), Buffer.from(await file.arrayBuffer()));
  }
}
Warning

Attachments come from strangers. Never trust filename as a path (sanitize it first), check content_type and size, and scan files before you open them.

Attachments are kept for your plan's data retention period.