> ## Documentation Index
> Fetch the complete documentation index at: https://developers.luccasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Started

> Introduction to the Files Management Sub-system.

The Files Management Sub-system allows you to manage file resources within the Lucca Public API. A file resource represents a file that can be uploaded, stored, and retrieved through the API.

A file can either be retrieved as a **JSON representation of its metadata** ; or **downloaded as a binary stream**.

## Getting Access to a File

In order to get access to a file, either its JSON representation or its actual content, you need:

* to authenticate with a valid access token (see [Authentication](https://developers.luccasoftware.com/documentation/using-api/authentication.md));
* to have either `files.readonly` or `files.readwrite` scope granted to your access token;
* to have a complementary token, that can only be given in the context of the API object that is related to this file and that must be passed in query parameters in order to `GET /files/{id}`.

This complementary token can only be retrieved via the resource that directly references this file, and it is bound to the API client that read that resource: follow the link with the same `Authorization` header. The token does not authenticate you on its own, and another API client cannot use it.

### Download Links: `links.files`

Every resource that references files (an employee-attribute whose value holds a file, an employment or a job-position with a `document`, an employee with a `portrait`) returns their download links under `links.files`, indexed by file id, when you send `?include=links`.

Each entry holds an `href` that returns the file content, the `token` it carries, and the `expiresAt` instant past which it stops working. Read the resource again to get a fresh link; do not store `href`. An entry is `null` when you may not download that file: skip it.

`href` carries an explicit `disposition=attachment`, which supersedes your `Accept` header: following it always returns the file content, even with `Accept: application/json`. To get the file's JSON representation instead, build your own URL from the `token`, with `disposition=json` (see [below](#viewing-a-file-json-vs-raw-content)).

For example, an employee-attribute holding a scanned document:

```http theme={null}
GET /lucca-api/employee-attributes/175?include=links HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01

> Response 200 OK
Content-Type: application/json

{
    "id": "175",
    "type": "employee-attribute",
    "url": "https://example.ilucca.net/lucca-api/employee-attributes/175",
    "definition": {
        "id": "e_idDocument",
        "type": "employee-attribute-definition",
        "url": "https://example.ilucca.net/lucca-api/employee-attribute-definitions/e_idDocument"
    },
    "value": {
        "id": "9154c6a2-6105-4d5d-bfb6-5fd2d54037b3",
        "type": "file",
        "url": "https://example.ilucca.net/lucca-api/files/9154c6a2-6105-4d5d-bfb6-5fd2d54037b3"
    },
    ...
    "links": {
        "files": {
            "9154c6a2-6105-4d5d-bfb6-5fd2d54037b3": {
                "href": "https://example.ilucca.net/lucca-api/files/9154c6a2-6105-4d5d-bfb6-5fd2d54037b3?token=eyXXXXXXXX&disposition=attachment",
                "expiresAt": "2026-10-21T13:38:47Z",
                "token": "eyXXXXXXXX"
            }
        }
    }
}
```

```http Download theme={null}
GET /lucca-api/files/9154c6a2-6105-4d5d-bfb6-5fd2d54037b3?token=eyXXXXXXXX&disposition=attachment HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01

> Response 200 OK
Content-Type: image/png
Content-disposition: attachment; filename="id-document.png"
...
```

The `url` of a file reference (`"url": "https://example.ilucca.net/lucca-api/files/9154c6a2-6105-4d5d-bfb6-5fd2d54037b3"` above) does not give access to the file: always go through `links.files`. On a collection, each item holds the links to its own files.

| Response | Cause |
| :- | :- |
| `401` | No `Authorization` header, or an invalid or expired access token. |
| `403` | Missing `files.readonly` scope, or a `token` that is missing, expired, or minted for another API client. |

### Employee Portraits

An employee's `portrait` is a picture file: its `id` is the id of that file. Like any other file, download it through its entry in `links.files`, returned with `?include=links`.

```http theme={null}
GET /lucca-api/employees/416?include=links HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01

> Response 200 OK
Content-Type: application/json

{
    "id": "416",
    "type": "employee",
    "url": "https://example.ilucca.net/lucca-api/employees/416",
    "givenName": "John",
    "familyName": "Doe",
    "portrait": {
        "id": "2face165-5345-4cf3-ab05-5421a6b19df2",
        "type": "portrait",
        "url": "https://example.ilucca.net/lucca-api/portraits/2face165-5345-4cf3-ab05-5421a6b19df2"
    },

    ...
    "links": {
        "files": {
            "2face165-5345-4cf3-ab05-5421a6b19df2": {
                "href": "https://example.ilucca.net/lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2?token=eyXXXXXXXX&disposition=attachment",
                "expiresAt": "2026-10-21T13:38:47Z",
                "token": "eyXXXXXXXX"
            }
        }
    }
}
```

### Deprecated Download Means

Before `links.files`, files were downloaded through the following means. They still work, but are deprecated: use `links.files` instead.

| Deprecated | What it does |
| :- | :- |
| `links.document` of an employment or a job-position | Regular link to the `document` file, carrying its token. Returns the file's JSON representation, and does not tell when its token expires. |
| `links.portrait` of an employee | Regular link to the `portrait` file, carrying its token. Returns the file's JSON representation, and does not tell when its token expires. |
| `GET /lucca-api/portraits/{id}` | Returns the portrait's JSON representation, or the picture with `Accept: image/*`. |

To get the content through a deprecated link, set the `disposition` query parameter to `attachment` or `inline` (see below).

## Viewing a File: JSON vs Raw Content

When retrieving a file via `GET /files/{id}`, you can either get its JSON representation (metadata) or its raw content (binary stream).

By default, the API will return the file metadata in JSON format. To get the raw content of the file, you need to set the `Accept` header to `application/octet-stream` or another appropriate MIME type (typically the one returned by the JSON metada in `response.contentType`).

When the Accept header is cannot be set, you can also use the `disposition` query parameter to specify how the file should be handled by the client. When set to `inline` or `attachment`, the API will return the raw content of the file accordingly.

```http GET JSON (Accept) theme={null}
GET /lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2?token=eyXXXXXXXX HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01
Accept: */*

> Response 200 OK
Content-Type: application/json

{
    "id": "2face165-5345-4cf3-ab05-5421a6b19df2",
    "type": "file",
    "url": "https://example.ilucca.net/lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2",
    "fileName": "portrait.png",
    "contentType": "image/png",
    "size": 34567,
    "createdAt": "2024-01-15T10:20:30Z"
}
```

```http GET JSON (?disposition=json) theme={null}
GET /lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2?token=eyXXXXXXXX&disposition=json HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01

> Response 200 OK
Content-Type: application/json

{
    "id": "2face165-5345-4cf3-ab05-5421a6b19df2",
    "type": "file",
    "url": "https://example.ilucca.net/lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2",
    "fileName": "portrait.png",
    "contentType": "image/png",
    "size": 34567,
    "createdAt": "2024-01-15T10:20:30Z"
}
```

```http GET Content (Accept) theme={null}
GET /lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2?token=eyXXXXXXXX HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01
Accept: application/octet-stream
```

```http GET Content (?disposition=inline) theme={null}
GET /lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2?token=eyXXXXXXXX&disposition=inline HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01

> Response 200 OK
Content-disposition: inline; filename="portrait.png"
...
```

```http GET Content (?disposition=attachment) theme={null}
GET /lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2?token=eyXXXXXXXX&disposition=attachment HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01

> Response 200 OK
Content-disposition: attachment; filename="portrait.png"
...
```

## Uploading a File

To upload a file to Lucca, you can use the `POST /lucca-api/files` endpoint. You need to send a `multipart/form-data` request containing the file to be uploaded.

Make sure to authenticate with a valid access token that has the `files.readwrite` scope, as well as set up the multipart boundary and content correctly.

The stored file name comes from the `filename` parameter of the multipart `Content-Disposition` header, not from the name of the file on your disk. You are free to set it to any value: this lets you store a file under a meaningful name in a single request, without having to rename it beforehand.

```http POST theme={null}
POST /lucca-api/files HTTP/1.1
Host: example.ilucca.net
Authorization: Bearer XXX
Api-Version: 2024-11-01
Content-Type: multipart/form-data; boundary=----boundary7MA4YWxkTrZu0gW
------boundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="portrait.png"

...binary content...

------boundary7MA4YWxkTrZu0gW--

> Response 201 Created
Content-Type: application/json
Location: https://example.ilucca.net/lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2
{
    "id": "2face165-5345-4cf3-ab05-5421a6b19df2",
    "type": "file",
    "url": "https://example.ilucca.net/lucca-api/files/2face165-5345-4cf3-ab05-5421a6b19df2",
    "fileName": "portrait.png",
    "contentType": "image/png",
    "size": 34567,
    "createdAt": "2024-01-15T10:20:30Z"
}
```

```bash cURL (renaming on upload) theme={null}
# The file on disk is a raw scanner output ("SKM_C25824011512.pdf"),
# but it is stored under a meaningful name via the filename parameter.
curl --location 'https://example.ilucca.net/lucca-api/files' \
--header 'Authorization: Bearer XXX' \
--form 'file=@"/scans/SKM_C25824011512.pdf";filename="employment-contract-john-doe.pdf"'

# > Response 201 Created
# {
#     "id": "9b2c1e77-4d3a-4e21-8f0b-1c2d3e4f5a6b",
#     "type": "file",
#     "url": "https://example.ilucca.net/lucca-api/files/9b2c1e77-4d3a-4e21-8f0b-1c2d3e4f5a6b",
#     "fileName": "employment-contract-john-doe.pdf",
#     "contentType": "application/pdf",
#     "size": 84213,
#     "createdAt": "2024-01-15T10:20:30Z"
# }
```

> [!TIP]
>
> File names are sanitized on upload: the characters `< > [ ] { } " ' ; = |` and the backtick are replaced with an underscore (`_`).


## Related topics

- [Get Started](https://developers.luccasoftware.com/api-reference/latest/organization/get-started.md)
