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

# Upload Attachment

> Upload local files or files via URL to the end of an attachment field in a specified record

### Path

POST /api/table/\{tableId}/record/\{recordId}/\{fieldId}/uploadAttachment

### Request

#### Path Parameters

* tableId (string): The unique identifier of the table [(how to get)](/en/api-doc/get-id#tableid).
* recordId (string): The unique identifier of the record to update [(how to get)](/en/api-doc/get-id#recordid).
* fieldId (string): The ID of the attachment field to upload to [(how to get)](/en/api-doc/get-id#fieldid)

Attachment fields can contain multiple attachments. This API allows uploading one attachment at a time to the end of the cell.

To delete or reorder attachments, use the [Update Record API](/en/api-doc/record/update).

fieldId must be an attachment type field.

Through the API, uploaded attachments are limited to 100MB in the cloud version, with no limit in the self-hosted version.

#### Request Body

Type: formData

Parameters:

* **file (optional)**
  * Description: The record data to update
  * Type: Buffer or ReadStream
* **fileUrl (optional)**
  * Description: The URL to upload from
  * Type: String
  * Example: `https://example.com/image.jpg`
  * Note: Only one of file or fileUrl can be specified at a time. If both are specified, file takes precedence.

### Response

#### Success Response

* Status code: 201 Created
* Response body: Returns the updated record data.

**Example Response Body**

```json theme={null}
{
    "id": "rec123456789ABCDE",
    "fields": {
      "fld123456789ABCDE": [
        {
          "id": "act75TiSyhcS7hfrizW",
          "name": "example.jpg",
          "path": "table/example",
          "size": 392903,
          "token": "tokenxxxxx",
          "width": 976,
          "height": 1000,
          "mimetype": "image/jpeg",
          "presignedUrl": "https://app.teable.ai/preview/previewURL"
        }
      ],
    }
}
```

#### Error Responses

* Status code: 400 Bad Request: Request body format error or missing required fields.
* Status code: 404 Not Found: Specified tableId or recordId does not exist.

### Example Code

<CodeGroup>
  ```bash CURL theme={null}
  # Upload via file

  curl -X POST 'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment' \
    -H 'Authorization: Bearer __token__' \
    -H 'Content-Type: multipart/form-data' \
    -F 'file=@/path/to/your/file.jpg'

  # Upload via URL
  curl -X POST 'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment' \
    -H 'Authorization: Bearer __token__' \
    -H 'Content-Type: multipart/form-data' \
    -F 'fileUrl=https://example.com/image.jpg'
  ```

  ```js JS SDK theme={null}
  import { configApi, uploadAttachment } from '@teable/openapi';

  configApi({
    endpoint: 'https://app.teable.ai',
    token: '__token__',
  });

  // Node.js environment: Upload local file
  const fileStream = fs.createReadStream('/path/to/your/file.jpg');
  const response = await uploadAttachment('__tableId__', '__recordId__', '__fieldId__', fileStream);

  console.log(response.data);

  // Upload URL (works in both Node.js and browser environments)
  const response = await uploadAttachment('__tableId__', '__recordId__', '__fieldId__', 'https://example.com/image.jpg');

  console.log(response.data);

  // Browser environment: Upload file
  // Assuming there's a file input element: <input type="file" id="fileInput">
  document.getElementById('fileInput').addEventListener('change', async (event) => {
    const file = event.target.files[0];
    if (file) {
      const response = await uploadAttachment('__tableId__', '__recordId__', '__fieldId__', file);
      console.log(response.data);
    }
  });
  ```

  ```ts TypeScript theme={null}
  import FormData from 'form-data';
  import fs from 'fs';

  // Node.js environment: Upload local file
  const formData = new FormData();
  formData.append('file', fs.createReadStream('/path/to/your/file.jpg'));

  const response = await fetch('https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer __token__',
      ...formData.getHeaders()
    },
    body: formData
  });

  console.log(await response.json());

  // Upload URL (works in both Node.js and browser environments)
  const formDataUrl = new FormData();
  formDataUrl.append('fileUrl', 'https://example.com/image.jpg');

  const responseUrl = await fetch('https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer __token__',
      ...formDataUrl.getHeaders()
    },
    body: formDataUrl
  });

  console.log(await responseUrl.json());

  // Browser environment: Upload file
  // Assuming there's a file input element: <input type="file" id="fileInput">
  document.getElementById('fileInput').addEventListener('change', async (event: Event) => {
    const fileInput = event.target as HTMLInputElement;
    const file = fileInput.files?.[0];
    if (file) {
      const formData = new FormData();
      formData.append('file', file);

      const response = await fetch('https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment', {
        method: 'POST',
        headers: {
          'Authorization': 'Bearer __token__'
        },
        body: formData
      });

      console.log(await response.json());
    }
  });
  ```

  ```python Python theme={null}
  import requests
  import mimetypes
  import os

  # Upload local file
  file_path = '/path/to/your/file.jpg'
  with open(file_path, 'rb') as file:
      file_name = os.path.basename(file_path)
      mime_type, _ = mimetypes.guess_type(file_path)
      files = {'file': (file_name, file, mime_type)}
      response = requests.post(
          'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment',
          headers={
              'Authorization': 'Bearer __token__'
          },
          files=files
      )

  print(response.json())

  # Upload URL
  response_url = requests.post(
      'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment',
      headers={
          'Authorization': 'Bearer __token__'
      },
      data={'fileUrl': 'https://example.com/image.jpg'}
  )

  print(response_url.json())
  ```
</CodeGroup>
