# Files and folders

## Overview

The `/v2/files` endpoints support all types of file resource (File, Folder, ContractDocument).

- Use `multipart/form-data` to upload file content for a File or ContractDocument.
- Use `application/json` to create folders.
- Use `application/merge-patch+json` to rename or move file resources.


Regarding file resource types
When consuming this guide treat all file resource types **except Folder** the same unless explicitly stated otherwise.

## Uploading files and new versions (`multipart/form-data`)

All file uploads using the LawVu API requires the use of `multipart/form-data` for uploading files. This allows you to send both file content and metadata in a single request.

### File upload resource types

When uploading a new file using `POST /v2/files`, provide what resource the file belongs to using `targetResourceType` and `targetResourceId` form fields.

The following table describes the supported values.

| `targetResourceType` | `targetResourceId` |  |
|  --- | --- | --- |
| Matter | {matterId} | Uploads the file to the matter with the ID {matterId} |
| Contract | {contractId} | Uploads the file to the contract with the ID {contractId} |
| ContractCreation |  | Uploads the file to be passed for creating a contract |


### 1. Creating a file on a matter

To create a new file on a matter with the ID 12345, the following request can be used:

**Request:**

```http
POST /v2/files
Authorization: Bearer <your_access_token>
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.pdf"
Content-Type: application/pdf

<file content here>
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceType"

Matter
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceId"

12345
------WebKitFormBoundary7MA4YWxkTrZu0gW--
```

### 2. Creating a file on a contract

To create a file on a contract with the ID 67890, use the following request:

**Request:**

```http
POST /v2/files
Authorization: Bearer <your_access_token>
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.pdf"
Content-Type: application/pdf

<file content here>
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceType"

Contract
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceId"

67890
------WebKitFormBoundary7MA4YWxkTrZu0gW--
```

### 3. Creating a file for contract creation

To upload a file to be used during contract creation, use the following request:

**Request:**

```http
POST /v2/files
Authorization: Bearer <your_access_token>
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.pdf"
Content-Type: application/pdf

<file content here>
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceType"

ContractCreation
------WebKitFormBoundary7MA4YWxkTrZu0gW--
```

### 4. Uploading a new version to an existing file

To upload a new version of an existing file, use the following request:
Replace `{fileId}` with the ID of the file you want to update.

**Request:**

```http
POST /v2/files/{fileId}
Authorization: Bearer <your_access_token>
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.pdf"
Content-Type: application/pdf

<file content here>
------WebKitFormBoundary7MA4YWxkTrZu0gW--
```

### 5. Uploading a file to a folder

To upload a new file to a folder on an existing matter or contract, add `folderId` to the multipart form body.

**Request:**

```http
POST /v2/files
Authorization: Bearer <your_access_token>
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.pdf"
Content-Type: application/pdf

<file content here>
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceType"

Contract
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="targetResourceId"

67890
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="folderId"

45890
------WebKitFormBoundary7MA4YWxkTrZu0gW--
```

## Creating folders (`application/json`)

Use `POST /v2/files` with `Content-Type: application/json`.

### Create folder at resource root

```http
POST /v2/files
Authorization: Bearer <your_access_token>
Content-Type: application/json

{
  "name": "Legal Documents",
  "targetResourceType": "Matter",
  "targetResourceId": 12345
}
```

### Create folder within an existing folder

```http
POST /v2/files
Authorization: Bearer <your_access_token>
Content-Type: application/json

{
  "name": "Drafts",
  "folderId": 45890,
  "targetResourceType": "Matter",
  "targetResourceId": 12345
}
```

## Updating files and folders (`PATCH`)

Use `PATCH /v2/files/{fileId}` with `Content-Type: application/merge-patch+json` to rename or move files/folders.

Note: Moving a file resource of type "ContractDocument" is not supported.

### Rename file or folder

```http
PATCH /v2/files/9456
Authorization: Bearer <your_access_token>
Content-Type: application/merge-patch+json

{
  "name": "Q4 Legal Review"
}
```

### Move file or folder into another folder

```http
PATCH /v2/files/9456
Authorization: Bearer <your_access_token>
Content-Type: application/merge-patch+json

{
  "folder": {
    "id": 786
  }
}
```

### Move file or folder back to root of the resource

```http
PATCH /v2/files/9456
Authorization: Bearer <your_access_token>
Content-Type: application/merge-patch+json

{
  "folder": null
}
```

For full schema details and response examples, refer to the [Files API documentation](https://api-docs.lawvu.com/new-api/lawvuapi.openapi/files).