> For the complete documentation index, see [llms.txt](https://developers.docstudio.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.docstudio.com/api-reference/upload-attachments.md). Agents can also search and read this documentation through its [MCP server](https://developers.docstudio.com/~gitbook/mcp).

# Upload Attachments

This document contains details for the /api/v1/envelope/attachment endpoint, including headers, request body, response fields, and status codes.

### Endpoint

| Method | Endpoint                      |
| ------ | ----------------------------- |
| `POST` | `/api/v1/envelope/attachment` |

### Headers

<table><thead><tr><th width="222">Header</th><th width="211">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>Authorization</code></td><td>Yes</td><td>Bearer authorization token</td></tr><tr><td><code>Content-Type</code></td><td>Yes</td><td><code>multipart/form-data</code></td></tr><tr><td><code>Mailbox</code></td><td>No</td><td>UUID of the current mailbox context</td></tr></tbody></table>

### Query parameters

<table><thead><tr><th width="115">Parameter</th><th width="86">Type</th><th width="106">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>type</code></td><td>string</td><td>No</td><td>File category: <code>all</code>, <code>document</code>, <code>file</code>, <code>image</code>, or <code>archive</code></td></tr></tbody></table>

### Request body

Send the file as `multipart/form-data` in the required `file` field.

<table><thead><tr><th width="91">Field</th><th width="101">Type</th><th width="220">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>file</code></td><td>binary</td><td>Yes</td><td>File to upload</td></tr></tbody></table>

### Request example

{% tabs %}
{% tab title="Node.js" %}

```javascript
import { openAsBlob } from "node:fs";

const file = await openAsBlob("contract.pdf", {
  type: "application/pdf"
});

const form = new FormData();
form.append("file", file, "contract.pdf");

const response = await fetch(
  "https://api.docstudio.com/api/v1/envelope/attachment?type=document",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer {token}",
      Mailbox: "{mailboxUuid}"
    },
    body: form
  }
);
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$file = new CURLFile(
    "contract.pdf",
    "application/pdf",
    "contract.pdf"
);

$client = curl_init(
    "https://api.docstudio.com/api/v1/envelope/attachment?type=document"
);

curl_setopt_array($client, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => ["file" => $file],
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer {token}",
        "Mailbox: {mailboxUuid}"
    ],
    CURLOPT_RETURNTRANSFER => true
]);

$response = curl_exec($client);
curl_close($client);
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://api.docstudio.com/api/v1/envelope/attachment?type=document"
headers = {
    "Authorization": "Bearer {token}",
    "Mailbox": "{mailboxUuid}"
}

with open("contract.pdf", "rb") as file:
    response = requests.post(
        url,
        headers=headers,
        files={"file": ("contract.pdf", file, "application/pdf")}
    )
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require "net/http"
require "securerandom"

uri = URI("https://api.docstudio.com/api/v1/envelope/attachment?type=document")
boundary = "----DocStudio#{SecureRandom.hex(16)}"

body = [
  "--#{boundary}",
  'Content-Disposition: form-data; name="file"; filename="contract.pdf"',
  "Content-Type: application/pdf",
  "",
  File.binread("contract.pdf"),
  "--#{boundary}--",
  ""
].join("\r\n")

request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer {token}"
request["Mailbox"] = "{mailboxUuid}"
request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
request.body = body

response = Net::HTTP.start(
  uri.hostname,
  uri.port,
  use_ssl: uri.scheme == "https"
) { |http| http.request(request) }
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
    "bytes"
    "io"
    "log"
    "mime/multipart"
    "net/http"
    "os"
)

func main() {
    file, err := os.Open("contract.pdf")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    var body bytes.Buffer
    writer := multipart.NewWriter(&body)
    part, err := writer.CreateFormFile("file", "contract.pdf")
    if err != nil {
        log.Fatal(err)
    }

    if _, err = io.Copy(part, file); err != nil {
        log.Fatal(err)
    }
    writer.Close()

    request, err := http.NewRequest(
        http.MethodPost,
        "https://api.docstudio.com/api/v1/envelope/attachment?type=document",
        &body,
    )
    if err != nil {
        log.Fatal(err)
    }

    request.Header.Set("Authorization", "Bearer {token}")
    request.Header.Set("Mailbox", "{mailboxUuid}")
    request.Header.Set("Content-Type", writer.FormDataContentType())

    response, err := http.DefaultClient.Do(request)
    if err != nil {
        log.Fatal(err)
    }
    defer response.Body.Close()
}
```

{% endtab %}

{% tab title="Java" %}

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;

public class UploadAttachment {
    public static void main(String[] args) throws Exception {
        String boundary = "----DocStudioBoundary";
        String lineBreak = "\r\n";
        Path file = Path.of("contract.pdf");

        HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.concat(
            HttpRequest.BodyPublishers.ofString(
                "--" + boundary + lineBreak +
                "Content-Disposition: form-data; name=\"file\"; " +
                "filename=\"contract.pdf\"" + lineBreak +
                "Content-Type: application/pdf" + lineBreak + lineBreak
            ),
            HttpRequest.BodyPublishers.ofFile(file),
            HttpRequest.BodyPublishers.ofString(
                lineBreak + "--" + boundary + "--" + lineBreak
            )
        );

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(
                "https://api.docstudio.com/api/v1/envelope/attachment?type=document"
            ))
            .header("Authorization", "Bearer {token}")
            .header("Mailbox", "{mailboxUuid}")
            .header(
                "Content-Type",
                "multipart/form-data; boundary=" + boundary
            )
            .POST(body)
            .build();

        HttpResponse<String> response = HttpClient.newHttpClient().send(
            request,
            HttpResponse.BodyHandlers.ofString()
        );
    }
}
```

{% endtab %}

{% tab title=".NET" %}

```csharp
using System.Net.Http.Headers;

using var client = new HttpClient();
using var form = new MultipartFormDataContent();
using var stream = File.OpenRead("contract.pdf");
using var file = new StreamContent(stream);

file.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
form.Add(file, "file", "contract.pdf");

using var request = new HttpRequestMessage(
    HttpMethod.Post,
    "https://api.docstudio.com/api/v1/envelope/attachment?type=document"
)
{
    Content = form
};

request.Headers.Authorization = new AuthenticationHeaderValue(
    "Bearer",
    "{token}"
);
request.Headers.Add("Mailbox", "{mailboxUuid}");

using var response = await client.SendAsync(request);
```

{% endtab %}

{% tab title="cURL" %}

```bash
curl --request POST \
  "https://api.docstudio.com/api/v1/envelope/attachment?type=document" \
  --header "Authorization: Bearer {token}" \
  --header "Mailbox: {mailboxUuid}" \
  --form "file=@contract.pdf;type=application/pdf"
```

{% endtab %}
{% endtabs %}

Let the HTTP client generate the multipart boundary automatically.

### Response

The endpoint returns `201 Created` with the uploaded attachment details.

```json
{
  "filename": "contract.pdf",
  "attachmentId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "filesize": 204800,
  "contentType": "application/pdf",
  "updatedPdf": "string"
}
```

<table><thead><tr><th width="153">Field</th><th width="187">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>filename</code></td><td>string</td><td>Name of the uploaded file</td></tr><tr><td><code>attachmentId</code></td><td>UUID</td><td>Identifier of the uploaded attachment</td></tr><tr><td><code>filesize</code></td><td>integer</td><td>Uploaded file size in bytes</td></tr><tr><td><code>contentType</code></td><td>string</td><td>MIME type of the uploaded file</td></tr><tr><td><code>updatedPdf</code></td><td>byte string</td><td>Updated PDF data when included in the response</td></tr></tbody></table>

### Attachment references

Set `attachmentUuid` to the returned `attachmentId`.

#### External document

Use the attachment as an external envelope document:

```xml
<document
  id="{documentId}"
  attachmentUuid="{attachmentId}"
/>
```

#### File field

Use the attachment as a file field value:

```xml
<document id="{documentId}">
  <field
    name="File"
    attachmentUuid="{attachmentId}"
  >contract.pdf</field>
</document>
```

### Status codes

<table><thead><tr><th width="324">Code</th><th>Description</th></tr></thead><tbody><tr><td><code>201</code></td><td>Attachment uploaded successfully</td></tr><tr><td><code>400</code></td><td>Invalid request body, query parameter, or headers</td></tr><tr><td><code>401</code></td><td>Missing, invalid, or expired authorization token</td></tr><tr><td><code>403</code></td><td>No access to the specified mailbox</td></tr><tr><td><code>413</code></td><td>Uploaded file exceeds the allowed size</td></tr></tbody></table>

### Related topics

* [XML payloads](https://developers.docstudio.com/guides/xml-payloads.md)
* [Templates and reusable IDs](https://developers.docstudio.com/guides/templates-and-reusable-ids.md)
* [Envelope fields](https://developers.docstudio.com/guides/envelope-fields.md)
* [Send your first envelope](https://developers.docstudio.com/introduction/send-your-first-envelope.md)
* [Send Envelope](https://developers.docstudio.com/api-reference/send-envelope.md)
* [Fill Envelope](https://developers.docstudio.com/api-reference/fill-envelope.md)
