# Files

List, search, upload, download, share and organise OneDrive files, or use a OneDrive file as post media.

Every call takes the `accountId` of a Microsoft account connected with `scopes=files`. Ids are OneDrive item ids. The account reaches every file the user can see in their OneDrive, so there is no picker step.

## List and search files

`GET /v1/files?accountId=…` lists the root of the user's OneDrive. Add `folderId` to list one folder, or `q` to search by name and content:

```bash
curl "https://zernio.com/api/v1/files?accountId=ACCOUNT_ID&q=invoice&limit=20" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```

```json
{
  "data": [
    {
      "id": "01BYE5RZ…",
      "accountId": "ACCOUNT_ID",
      "provider": "microsoft",
      "name": "invoice-october.pdf",
      "kind": "file",
      "mimeType": "application/pdf",
      "size": 48213,
      "downloadable": true,
      "webUrl": "https://contoso-my.sharepoint.com/personal/…"
    }
  ],
  "pagination": { "hasMore": false, "nextCursor": null }
}
```

`GET /v1/files/{fileId}?accountId=…` returns one file. `limit` goes up to 100, default 50.

## Download a file

`GET /v1/files/{fileId}/download?accountId=…` streams the file's bytes with its type and filename. Folders have `downloadable: false`.

## Upload a file

`POST /v1/files` uploads from a URL as JSON, or a file as `multipart/form-data`:

```bash
curl -X POST "https://zernio.com/api/v1/files" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "accountId": "ACCOUNT_ID", "name": "report.pdf", "contentType": "application/pdf", "url": "https://example.com/report.pdf", "parentId": "FOLDER_ID" }'
```

Files over 4 MB are uploaded to OneDrive in chunks. `convertTo` is Google-only: OneDrive refuses it.

## Organise and share

- `POST /v1/files/folders` creates a folder, with `name` and an optional `parentId`.
- `PATCH /v1/files/{fileId}` renames a file (`name`) or moves it (`parentId`).
- `DELETE /v1/files/{fileId}?accountId=…` moves it to the OneDrive recycle bin.
- `GET /v1/files/{fileId}/share-link?accountId=…` creates an anonymous view link that anyone with it can open. Organizations that turn off anonymous links refuse it.

## Use a OneDrive file in a post

A post's `mediaItems` accepts `{ "fileId": "…", "accountId": "…" }` next to the usual `url` items.

Zernio copies the file when the post is created, so it publishes even if the file changes or the account disconnects later. A folder answers `400` on that media item.

## How it behaves

- Picker sessions are Google-only. `POST /v1/files/picker-sessions` on a Microsoft account answers `400` `platform_not_supported`.
- Uploads by URL accept up to 100 MB, over https only, without redirects, and must download within 20 seconds. Multipart uploads are limited by the 4.5 MB request size, so send larger files by URL.
- Names are up to 255 characters, without `/` or `\`.
- `file.created`, `file.updated` and `file.deleted` fire for changes in the user's OneDrive. See [Webhooks](/platforms/microsoft/webhooks).

## If it fails

| Error | Cause | Fix |
|-------|-------|-----|
| `400` on `url` | The URL is not https, redirects, or the file is over 100 MB | Use a direct https link to a smaller file. |
| `400` on `convertTo` | OneDrive cannot convert files | Leave `convertTo` out. |
| `404` `resource_not_found` | The file was deleted, or the id is wrong | List the folder again. |
| `412` `account_scope_missing` | The account has no `files` area | [Connect again](/platforms/microsoft/connect#add-areas-later) with `files` in `scopes`. |
| `412` `admin_consent_required` | The organization requires an administrator's approval | Follow [admin consent](/platforms/microsoft/admin-consent). |

## Related

- [List files](/files/list-files)
- [Upload a file](/files/upload-file)
- [Download a file](/files/download-file)
- [Get a share link](/files/get-file-share-link)