Attach Files to an Issue
Use Case
Use the REST API to upload one or more files to an existing issue.
Summary
To attach a file to an issue:
Get a list of issues to get the entity ID of the target issue.
Attach files to the issue by sending a POST request to the following endpoint:
/api/issues/{issueID}/attachments
Step-by-Step
Get the list of issues in a target project.
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 perm:cm9vdA==.MjZGZWI=.WB02vjX0cM2ltLTJXUE3VOWHpJYYNx'The
queryrequest 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=3parameter to get only first three found issues.In the
fieldsrequest parameter, we instruct the server to return the following attributes for each issue found:idandidReadable- 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:
[ { "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" } ]Locate the ID of the target issue. In this case, let's take the issue
NP-77with the database ID99-500.To attach files to an issue, send a
POSTrequest with the file content to the following endpoint:/api/issues/{issueID}/attachmentsThis endpoint expects file upload data in
multipart/form-dataformat. Use this request pattern when you upload a file usingmultipart/form-data, for example with thecURL -F upload=@...option.For this use case, we use the following
cURLcommand:curl -v -i -F upload=@/Users/jetbrains/Downloads/youtrack.txt \ -F upload=@/Users/jetbrains/Downloads/youtrack_new.jpeg \ -H 'Authorization: Bearer perm:cm9vdA==.MjZGZWI=.WB02vjX0cM2ltLTJXUE3VOWHpJYYNx' \ -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 upload=@/Users/jetbrains/Downloads/youtrack.txtuploads a file as a multipart form field namedupload.To attach multiple files in one request, repeat the
-F upload=@...option for each file:-F upload=@/Users/jetbrains/Downloads/youtrack.txt \ -F upload=@/Users/jetbrains/Downloads/youtrack_new.jpegThe
@prefix tellscURLto read the file content from the local filesystem.Content-Type: multipart/form-datais the correct content type for this upload pattern.
In the response, the server returns the
nameand database entity ID of the attached files:[ {"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.