> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.middesk.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.middesk.com/_mcp/server.

# Upload a document

POST https://api.middesk.com/v1/documents
Content-Type: application/json

Attaches a customer-supplied document to a business or an agent thread. The file must be base64-encoded, at most 10 MB, and a PDF, JPEG, or PNG. The content type is detected from the file bytes, not the filename.

Reference: https://docs.middesk.com/api-reference/documents/create-document

## Authentication

- `Authorization` header (bearer token, required) — Bearer authentication of the form `Bearer <token>`, where token is your auth token.

## Request

### Body (application/json)

This endpoint expects an object.

- `file` (string, required) — Base64-encoded file contents. The decoded file must be at most 10 MB and its content type, detected from the bytes rather than the filename, must be application/pdf, image/jpeg, or image/png.
- `filename` (string, required) — Name of the file, including its extension
- `documentable_type` (enum, required) — Type of record to attach the document to. Use `businesses` with a business ID, or `threads` with an agent thread ID.
  - Allowed values: `businesses`, `threads`
- `documentable_id` (string, required) — ID of the business or agent thread to attach the document to

## Response

### 201

document created

- `object` (enum, required)
  - Allowed values: `document`
- `id` (string, required)
- `document_type` (string, required, nullable) — Type of document. Common values include: Initial_Filing, Certificate of Good Standing, Articles of Incorporation, Initial Filing (UCC1), Amendment (UCC3). Null for customer-uploaded documents.
- `filename` (string, required) — Name of the document file
- `content_type` (string, required) — MIME type of the document
- `size` (integer, required) — File size in bytes
- `download_url` (string, required) — URL to download the document
- `created_at` (datetime, required)
- `source` (DocumentSource, optional)
- `filing_date` (datetime, optional, nullable)
- `metadata` (map from string to any, optional) — Additional document metadata

## Errors

### 400 Create Document Request Bad Request Error

invalid documentable_type

- `errors` (list of ErrorResponseErrorsItem, required)

### 404 Create Document Request Not Found Error

business not found

- `errors` (list of ErrorResponseErrorsItem, required)

### 422 Create Document Request Unprocessable Entity Error

file is larger than 10 MB or is not a PDF, JPEG, or PNG

- `errors` (list of ErrorResponseErrorsItem, required)

## Types

### DocumentSource

- `type` (string, optional) — Source type (e.g., registration)
- `id` (string, optional) — Source record ID
- `metadata` (map from string to any, optional) — Additional source metadata

### ErrorResponseErrorsItem

- `message` (string, required)
- `parameter` (string, optional) — Request parameter associated with the error, when applicable

## Examples

### Business document

**Request**

```json
{
  "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
  "filename": "irs_letter.pdf",
  "documentable_type": "businesses",
  "documentable_id": "00000000-0000-0000-0000-000000000001"
}
```

**Response**

```json
{
  "object": "document",
  "id": "00000000-0000-0000-0000-000000000000",
  "document_type": null,
  "filename": "irs_letter.pdf",
  "content_type": "application/pdf",
  "size": 3028,
  "download_url": "https://middesk.test/document.pdf?expires_in=10&special=false",
  "created_at": "2024-01-15T12:00:00Z",
  "source": {
    "type": "business",
    "id": "00000000-0000-0000-0000-000000000001",
    "metadata": {
      "status": "in_review"
    }
  },
  "metadata": {
    "key": "value"
  }
}
```

**SDK Code**

```python Business document
import requests

url = "https://api.middesk.com/v1/documents"

payload = {
    "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
    "filename": "irs_letter.pdf",
    "documentable_type": "businesses",
    "documentable_id": "00000000-0000-0000-0000-000000000001"
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript Business document
const url = 'https://api.middesk.com/v1/documents';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"file":"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==","filename":"irs_letter.pdf","documentable_type":"businesses","documentable_id":"00000000-0000-0000-0000-000000000001"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go Business document
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.middesk.com/v1/documents"

	payload := strings.NewReader("{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"businesses\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000001\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby Business document
require 'uri'
require 'net/http'

url = URI("https://api.middesk.com/v1/documents")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"businesses\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000001\"\n}"

response = http.request(request)
puts response.read_body
```

```java Business document
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.middesk.com/v1/documents")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"businesses\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000001\"\n}")
  .asString();
```

```php Business document
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.middesk.com/v1/documents', [
  'body' => '{
  "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
  "filename": "irs_letter.pdf",
  "documentable_type": "businesses",
  "documentable_id": "00000000-0000-0000-0000-000000000001"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp Business document
using RestSharp;

var client = new RestClient("https://api.middesk.com/v1/documents");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"businesses\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000001\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Business document
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
  "filename": "irs_letter.pdf",
  "documentable_type": "businesses",
  "documentable_id": "00000000-0000-0000-0000-000000000001"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.middesk.com/v1/documents")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```

### Thread document

**Request**

```json
{
  "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
  "filename": "irs_letter.pdf",
  "documentable_type": "threads",
  "documentable_id": "00000000-0000-0000-0000-000000000002"
}
```

**Response**

```json
{
  "object": "document",
  "id": "00000000-0000-0000-0000-000000000003",
  "document_type": null,
  "filename": "irs_letter.pdf",
  "content_type": "application/pdf",
  "size": 3028,
  "download_url": "https://middesk.test/document.pdf?expires_in=10&special=false",
  "created_at": "2024-01-15T12:00:00Z",
  "source": {
    "type": "thread",
    "id": "00000000-0000-0000-0000-000000000002",
    "metadata": {
      "status": "running"
    }
  },
  "metadata": {
    "key": "value"
  }
}
```

**SDK Code**

```python Thread document
import requests

url = "https://api.middesk.com/v1/documents"

payload = {
    "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
    "filename": "irs_letter.pdf",
    "documentable_type": "threads",
    "documentable_id": "00000000-0000-0000-0000-000000000002"
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript Thread document
const url = 'https://api.middesk.com/v1/documents';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"file":"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==","filename":"irs_letter.pdf","documentable_type":"threads","documentable_id":"00000000-0000-0000-0000-000000000002"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go Thread document
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.middesk.com/v1/documents"

	payload := strings.NewReader("{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"threads\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000002\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby Thread document
require 'uri'
require 'net/http'

url = URI("https://api.middesk.com/v1/documents")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"threads\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000002\"\n}"

response = http.request(request)
puts response.read_body
```

```java Thread document
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.middesk.com/v1/documents")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"threads\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000002\"\n}")
  .asString();
```

```php Thread document
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.middesk.com/v1/documents', [
  'body' => '{
  "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
  "filename": "irs_letter.pdf",
  "documentable_type": "threads",
  "documentable_id": "00000000-0000-0000-0000-000000000002"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp Thread document
using RestSharp;

var client = new RestClient("https://api.middesk.com/v1/documents");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"file\": \"JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==\",\n  \"filename\": \"irs_letter.pdf\",\n  \"documentable_type\": \"threads\",\n  \"documentable_id\": \"00000000-0000-0000-0000-000000000002\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Thread document
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "file": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQ==",
  "filename": "irs_letter.pdf",
  "documentable_type": "threads",
  "documentable_id": "00000000-0000-0000-0000-000000000002"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.middesk.com/v1/documents")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```