# Download an Attachment

> **GET** `https://api.us.nylas.com/v3/grants/{grant_id}/attachments/{attachment_id}/download`

Source: https://developer.nylas.com/docs/reference/api/attachments/get-attachments-id-download/

Returns and downloads the specified attachment.

**Authentication:** NYLAS_API_KEY, ACCESS_TOKEN

## Parameters

### Query parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `query_imap` | boolean | No | (IMAP, Yahoo, and iCloud only) When `true`, Nylas downloads the attachment directly from the IMAP server instead of the Nylas database. |
| `message_id` | string | Yes | ID of the message the specified attachment belongs to. |

### Path parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `grant_id` | string | Yes | ID of the grant to access. Use `/me/` to refer to the grant associated with an access token. |
| `attachment_id` | string | Yes | ID of the attachment to access. Nylas recommends you URL-encode this field, or you might receive a [`404` error](/docs/api/errors/400-response/) if the ID contains special characters (for example, `#`). |

## Responses

### 200 - The attached file as a binary stream (`application/octet-stream`). Use the
[Return Attachment Metadata endpoint](/docs/reference/api/attachments/get-attachments-id/)
to get the MIME type of the file.



### 400 - Bad Request

- `request_id` (string) - The request ID.
- `error` (object) - The response error object.
  - `type` (string) - The error type.
  - `message` (string) - The error message.
  - `provider_error` (object) - The error from the provider.

### 401 - Unauthorized

- `request_id` (string) - The request ID.
- `error` (object) - The response error object.
  - `type` (string) - The error type.
  - `message` (string) - The error message.
  - `provider_error` (object) - The error from the provider.

### 404 - Not Found

- `request_id` (string) - The request ID.
- `error` (object) - The response error object.
  - `type` (string) - The error type.
  - `message` (string) - The error message.
  - `provider_error` (object) - The raw error from the provider, if available
    - `code` (string)
    - `message` (string)

### 429 - Rate Limit

- `request_id` (string) - The request ID.
- `error` (object) - The response error object.
  - `type` (string) - The error type.
  - `message` (string) - The error message.

### 504 - Provider Failure

- `request_id` (string) - The request ID.
- `error` (object) - The response error object.
  - `type` (string) - The error type.
  - `message` (string) - The error message.

## Code samples

### cURL

```bash
curl --compressed --request GET \
  --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/attachments/<ATTACHMENT_ID>/download?message_id=<MESSAGE_ID>' \
  --header 'Authorization: Bearer <NYLAS_API_KEY>'

```

### Node.js SDK

```javascript
import fs from "fs";
import Nylas from "nylas";

const nylas = new Nylas({
  apiKey: "<NYLAS_API_KEY>",
  apiUri: "<NYLAS_API_URI>",
});

async function downloadAttachment() {
  try {
    const attachmentBytes = await nylas.attachments.downloadBytes({
      identifier: "<NYLAS_GRANT_ID>",
      attachmentId: "<ATTACHMENT_ID>",
      queryParams: {
        messageId: "<MESSAGE_ID>",
      },
    });

    const fileName = "attachment";
    await fs.promises.writeFile(fileName, attachmentBytes);
    console.log(`File saved as ${fileName}`);
  } catch (error) {
    console.error("Error fetching attachment:", error);
  }
}

downloadAttachment();

```

### Python SDK

```python
from nylas import Client

nylas = Client(
    "<NYLAS_API_KEY>",
    "<NYLAS_API_URI>"
)

grant_id = "<NYLAS_GRANT_ID>"
attachment_id = "<ATTACHMENT_ID>"

attachment = nylas.attachments.download(
  grant_id,
  attachment_id,
  query_params= {
    "message_id": "<MESSAGE_ID>",
  }
)

with open("attachment", 'wb') as f:
  f.write(attachment.content)

```

### Ruby SDK

```ruby
require 'nylas'

nylas = Nylas::Client.new(
    api_key: "<NYLAS_API_KEY>"
)

query_params = {
  message_id: "<MESSAGE_ID>"
}

attachment = nylas.attachments.download(identifier: "<NYLAS_GRANT_ID>", 
    attachment_id: "<ATTACHMENT_ID>", query_params: query_params)

File.open("./image.png", "wb") do |file|
  file.write(attachment)
end
```

### Java SDK

```java
import com.nylas.NylasClient;
import com.nylas.models.*;
import okhttp3.ResponseBody;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class attachment_download {
  public static void main(String[] args) throws NylasSdkTimeoutError, NylasOAuthError, IOException {
    NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build();
    FindAttachmentQueryParams queryParams = new FindAttachmentQueryParams("<MESSAGE_ID>");
    ResponseBody attachment = nylas.attachments().download("<NYLAS_GRANT_ID>", "<ATTACHMENT_ID>", queryParams);

    try {
      FileOutputStream out = new FileOutputStream("src/main/resources/image.png");
      
      out.write(attachment.bytes());
      out.close();
    } catch (FileNotFoundException e) {
      System.out.println("File not found");
    }
  }
}
```

### Kotlin SDK

```kotlin
import com.nylas.NylasClient
import com.nylas.models.FindAttachmentQueryParams
import java.io.File

fun main(args: Array<String>) {
  val nylas: NylasClient = NylasClient(
      apiKey = "<NYLAS_API_KEY>"
  )

  val queryParams = FindAttachmentQueryParams("<MESSAGE_ID>")
  val attachment = nylas.attachments().download("<NYLAS_GRANT_ID>", "<ATTACHMENT_ID>", queryParams)
  
  File("src/main/resources/Image.png").writeBytes(attachment.bytes())
}
```
