# Download Attachment — Gmail

> Download a Gmail message attachment to /tmp and return its path + metadata. File Stash syncs the file and exposes a presigned download URL so the caller can retrieve it. Call Find Emails (with format: "full") or Get Thread first…

- Key: `gmail-download-attachment`
- Type: Action (Read-only)
- Version: 0.1.3
- App: Gmail (`gmail`) — https://pipedream.com/apps/gmail.md
- This page (HTML): https://pipedream.com/apps/gmail/actions/download-attachment
- Hints: read-only · open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/gmail/actions/download-attachment/download-attachment.mjs

## Description

Download a Gmail message attachment to `/tmp` and return its path + metadata. File Stash syncs the file and exposes a presigned download URL so the caller can retrieve it. Call **Find Emails** (with `format: "full"`) or **Get Thread** first — attachment IDs only appear in full-format message reads; each returned message's `payload.parts[]` enumerates attachments as `{ body.attachmentId, filename, mimeType }`. Pass the enclosing message's `id` as `messageId` and the part's `body.attachmentId` as `attachmentId`. If `filename` is omitted, the action looks up the attachment's filename from the message payload. Set `convertToPdf: true` to convert image / HTML / plain-text / DOCX attachments to PDF during download; other MIME types are rejected. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.messages.attachments/get).

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `messageId` | `string` | Yes | The id of the message containing the attachment. Obtain this from Find Emails or Get Thread. |
| `attachmentId` | `string` | Yes | The attachment's ID. Find this on a message's payload.parts[].body.attachmentId — attachment IDs only appear when Find Emails is called with format: "full", or when using Get Thread (which always returns full payloads). |
| `filename` | `string` | No | Filename to save in /tmp (include the extension, e.g. report.pdf). If omitted, the action looks up the filename from the message payload. |
| `convertToPdf` | `boolean` | No | When true, convert the attachment to a PDF. Supported source MIME types: image/*, text/html, text/plain, and DOCX (application/vnd.openxmlformats-officedocument.wordprocessingml.document). Other types throw. |
| `syncDir` | `dir` | Yes | SyncDir |

## Run it

**MCP**

```ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const accessToken = await pd.rawAccessToken

const transport = new StreamableHTTPClientTransport(
  new URL("https://remote.mcp.pipedream.net/v3"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        "x-pd-project-id": process.env.PIPEDREAM_PROJECT_ID!,
        "x-pd-environment": "production",
        "x-pd-external-user-id": "{external_user_id}", // any stable ID for this user in your system
        "x-pd-app-slug": "gmail",
      },
    },
  },
)

const mcp = new Client({ name: "my-agent", version: "1.0.0" })
await mcp.connect(transport)

const { tools } = await mcp.listTools()

// listTools() hands your model this tool's input schema, so it can
// fill the arguments itself:
const result = await mcp.callTool({
  name: "gmail-download-attachment",
  arguments: {
    messageId: "Message ID",
    attachmentId: "Attachment ID",
  },
})
```

**TypeScript**

```ts
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const result = await pd.actions.run({
  id: "gmail-download-attachment",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    gmail: { authProvisionId: "apn_xxxxxxx" },
    messageId: "Message ID",
    attachmentId: "Attachment ID",
  },
})

console.log(result)
```

**cURL**

```bash
curl -X POST https://api.pipedream.com/v1/connect/{project_id}/actions/run \
  -H "Content-Type: application/json" \
  -H "X-PD-Environment: production" \
  -H "Authorization: Bearer {access_token}" \
  -d '{
    "external_user_id": "{external_user_id}",
    "id": "gmail-download-attachment",
    "configured_props": {
      "gmail": { "authProvisionId": "apn_xxxxxxx" },
      "messageId": "Message ID",
      "attachmentId": "Attachment ID"
    }
  }'
```

---

- App: https://pipedream.com/apps/gmail.md · All apps: https://pipedream.com/apps
