> For the complete documentation index, see [llms.txt](https://molgenis.gitbook.io/molgenis/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://molgenis.gitbook.io/molgenis/8.4/interoperability/guide-api-files.md).

# Files api

The Files API allows you do upload/download files.

## Upload file

### Request

```
POST /api/files
...binary data in the body...
```

#### Request Headers

| Name                | Description                                            | Required | Default value              |
| ------------------- | ------------------------------------------------------ | -------- | -------------------------- |
| Content-Type \*     | A standard MIME type describing the format of the file | No       | application/octet-stream   |
| Content-Length      | File size in bytes                                     | No       |                            |
| x-molgenis-filename | Filename                                               | No       | unknown                    |
| x-molgenis-token    | Authentication token                                   | No       | session cookie if supplied |

* All media types with the exception of `application/x-www-form-urlencoded` and `multipart/form-data` are allowed. See the JavaScript example for how to upload a file from a form file input.

  **Response**

  | Status code   | Description                                                                 |
  | ------------- | --------------------------------------------------------------------------- |
  | 201 Created   | File uploaded and file metadata created.                                    |
  | 403 Forbidden | If the authenticated user does not have WRITE permissions on file metadata. |

#### Response Headers

| Name     | Description                          |
| -------- | ------------------------------------ |
| Location | Location of a newly created resource |

#### Response body

Filemeta as returned by the 'Retrieve file metadata' endpoint.

### Examples

#### JavaScript

```javascript
var file = document.getElementById('my-file-input').files[0]
var request = new XMLHttpRequest()
request.open('post', '/api/files', true)
request.setRequestHeader("Content-Type", file.type)
request.setRequestHeader("Content-Length", file.size)
request.setRequestHeader("x-molgenis-filename", file.name)
request.send(file)
```

#### cURL

curl -H 'Content-Type: application/octet-stream' --data-binary @my.file /api/files

## Download file

### Request

```
GET /api/files/myFileId?alt=media
```

### Response

| Status code   | Description                                                                   |
| ------------- | ----------------------------------------------------------------------------- |
| 200 OK        | File downloaded                                                               |
| 403 Forbidden | If the authenticated user does not have READ permissions on the file metadata |
| 404 Not Found | If the file identifier is unknown                                             |

## Retrieve file metadata

### Request

```
GET /api/files/myFileId
```

### Response

| Status code   | Description                                                                   |
| ------------- | ----------------------------------------------------------------------------- |
| 200 OK        | File metadata                                                                 |
| 403 Forbidden | If the authenticated user does not have READ permissions on the file metadata |

#### Response body

Example:

```javascript
{
  "id": "aaaac2mp6yoxgaavluayxfiaae",
  "filename": "logo_green.png",
  "contentType": "image/png",
  "size": 11189,
}
```

## Delete file

### Request

```
DELETE /api/files/myFileId
```

### Response

| Status code    | Description                                                                    |
| -------------- | ------------------------------------------------------------------------------ |
| 204 No Content | File and filemeta deleted.                                                     |
| 403 Forbidden  | If the authenticated user does not have WRITE permissions on the file metadata |
| 404 Not Found  | If the file identifier is unknown                                              |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://molgenis.gitbook.io/molgenis/8.4/interoperability/guide-api-files.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
