# Attach Files to an Issue

Use the REST API to upload one or more files to an existing issue.

## Summary

To attach a file to an issue:

1. Get a list of issues to get the entity ID of the target issue.

2.

Attach files to the issue by sending a POST request to the following endpoint:

```
/api/issues/{issueID}/attachments
```

## Step-by-Step

Procedure:

1. Get the list of issues in a target project.

```CURL
curl -X GET 'https://example.youtrack.cloud/api/issues?query=in:NP&$top=3&fields=id,idReadable,summary' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YouTrack_token>'
```

The `query` request parameter lets us filter the list of issues by a search query. In this sample, we use the query to get only the issues that belong to the target project (`NP`).

For this particular example, to shorten the list of returned issues, we use `$top=3` parameter to get only first three found issues.

In the `fields` request parameter, we instruct the server to return the following attributes for each issue found:

* `id` and `idReadable` - both database entity ID and an ID of the issue in the project, respectively.

* `summary` - issue's summary may help to identify the target issue.

In the response to this request, the server returns the following data:

```JSON
[
    {
        "summary": "Investigation task",
        "idReadable": "NP-56",
        "id": "99-356",
        "$type": "Issue"
    },
    {
        "summary": "Marketing activities",
        "idReadable": "NP-39",
        "id": "99-303",
        "$type": "Issue"
    },
    {
        "summary": "TODO for release",
        "idReadable": "NP-77",
        "id": "99-500",
        "$type": "Issue"
    }
]
```

2. Locate the ID of the target issue. In this case, let's take the issue `NP-77` with the database ID `99-500`.

3. To attach files to an issue, send a `POST` request with the file content to the following endpoint:

```
/api/issues/{issueID}/attachments
```

This endpoint expects file upload data in `multipart/form-data` format. Use this request pattern when you upload a file using `multipart/form-data`, for example with the `cURL -F upload1=@...` option. When you upload several files in one request, use a separate multipart form field name for each file.

For this use case, we use the following `cURL` command:

```CURL
curl -v -i -F upload1=@/Users/jetbrains/Downloads/youtrack.txt \
-F upload2=@/Users/jetbrains/Downloads/youtrack_new.jpeg \
-H 'Authorization: Bearer <YouTrack_token>' \
-H 'Content-Type: multipart/form-data' \
-X POST 'https://example.youtrack.cloud/api/issues/99-500/attachments?fields=id,name'
```

Here are the key parts of this request:

* `-F upload1=@/Users/jetbrains/Downloads/youtrack.txt` uploads a file as a multipart form field named `upload1`.

* To attach multiple files in one request, add a `-F` option with a unique form field name for each file: ```CURL -F upload1=@/Users/jetbrains/Downloads/youtrack.txt \ -F upload2=@/Users/jetbrains/Downloads/youtrack_new.jpeg ```

* The `@` prefix tells `cURL` to read the file content from the local filesystem.

* `Content-Type: multipart/form-data` is the correct content type for this upload pattern.

> **Note:**
> This example describes multipart file upload. It is not a JSON-based request for base64-encoded attachment content.

In the response, the server returns the `name` and database entity ID of the attached files:

```JSON
[
  {"name":"youtrack.txt","id":"134-31","$type":"IssueAttachment"},
  {"name":"youtrack_new.jpeg","id":"134-32","$type":"IssueAttachment"}
]
```

That’s it: we’ve attached all our files to the issue.

> **Note:**
> Please note that you cannot simultaneously create a new issue and add attachments to it. You can attach files only to an already existing issue.

