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

# Presigned URL

> Generate a presigned PUT URL to upload an evidence file before submitting a response

# Presigned URL

Generate a presigned S3 PUT URL for uploading an evidence file. Use this before calling [Submit Response](/api-reference/custom-kpi/submit-response) — upload the file to the returned URL, then include the `file_id` in your submission's `attachment_file_ids`.

## Request

### Headers

<ParamField header="x-api-key" type="string" required>
  Your API key for authentication

  **Example:** `sk_live_1234567890abcdef`
</ParamField>

<ParamField header="x-organization-id" type="string" required>
  Your organization UUID

  **Example:** `a8315ef3-dd50-43f8-b7ce-d839e68d51fa`
</ParamField>

### Path Parameters

<ParamField path="dataset_id" type="string" required>
  UUID of the parent dataset
</ParamField>

### Body Parameters

<ParamField body="file_name" type="string" required>
  Original file name (1–255 characters)
</ParamField>

<ParamField body="content_type" type="string">
  MIME type of the file (e.g. `application/pdf`, `image/png`). Max 255 characters.
</ParamField>

<ParamField body="content_length_bytes" type="integer" required>
  File size in bytes. Must be between 1 and 10 485 760 (10 MB).
</ParamField>

## Response

Returns the presigned upload URL and file metadata (HTTP 201).

<ResponseField name="upload_url" type="string">Presigned S3 PUT URL — upload your file here with a PUT request</ResponseField>
<ResponseField name="file_id" type="string">File UUID — include this in `attachment_file_ids` when submitting</ResponseField>
<ResponseField name="file_name" type="string">Echoed file name</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  # 1. Get the presigned URL
  curl -X POST "https://api.dcycle.io/v1/custom-kpi-datasets/${DATASET_ID}/responses/attachments/presigned-url" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "file_name": "energy-audit-2025.pdf",
      "content_type": "application/pdf",
      "content_length_bytes": 2048576
    }'

  # 2. Upload the file to the presigned URL
  curl -X PUT "${UPLOAD_URL}" \
    -H "Content-Type: application/pdf" \
    --data-binary @energy-audit-2025.pdf
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import requests
  import os

  headers = {
      "x-api-key": os.getenv("DCYCLE_API_KEY"),
      "x-organization-id": os.getenv("DCYCLE_ORG_ID"),
      "Content-Type": "application/json",
  }

  dataset_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
  file_path = "energy-audit-2025.pdf"
  file_size = os.path.getsize(file_path)

  # Step 1: Get presigned URL
  presign = requests.post(
      f"https://api.dcycle.io/v1/custom-kpi-datasets/{dataset_id}/responses/attachments/presigned-url",
      headers=headers,
      json={
          "file_name": "energy-audit-2025.pdf",
          "content_type": "application/pdf",
          "content_length_bytes": file_size,
      },
  ).json()

  # Step 2: Upload the file
  with open(file_path, "rb") as f:
      requests.put(presign["upload_url"], data=f, headers={"Content-Type": "application/pdf"})

  print(f"Uploaded! file_id: {presign['file_id']}")
  # Use presign["file_id"] in attachment_file_ids when submitting
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const axios = require('axios');
  const fs = require('fs');

  const headers = {
    'x-api-key': process.env.DCYCLE_API_KEY,
    'x-organization-id': process.env.DCYCLE_ORG_ID,
    'Content-Type': 'application/json',
  };

  const datasetId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
  const fileBuffer = fs.readFileSync('energy-audit-2025.pdf');

  // Step 1: Get presigned URL
  const { data: presign } = await axios.post(
    `https://api.dcycle.io/v1/custom-kpi-datasets/${datasetId}/responses/attachments/presigned-url`,
    {
      file_name: 'energy-audit-2025.pdf',
      content_type: 'application/pdf',
      content_length_bytes: fileBuffer.length,
    },
    { headers }
  );

  // Step 2: Upload the file
  await axios.put(presign.upload_url, fileBuffer, {
    headers: { 'Content-Type': 'application/pdf' },
  });

  console.log(`Uploaded! file_id: ${presign.file_id}`);
  // Use presign.file_id in attachment_file_ids when submitting
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "upload_url": "https://dcycle-custom-kpi.s3.eu-west-1.amazonaws.com/attachments/...?X-Amz-Algorithm=AWS4-HMAC-SHA256&...",
  "file_id": "a1b2c3d4-0000-0000-0000-000000000001",
  "file_name": "energy-audit-2025.pdf"
}
```

## Common Errors

### 401 Unauthorized

**Cause:** Missing or invalid API key

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Invalid API key for organization", "code": "INVALID_API_KEY"}
```

### 403 Forbidden

**Cause:** The authenticated user is not a member of the organization

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Logged User is not Member of Organization", "code": "LOGGED_USER_NOT_MEMBER"}
```

### 422 Validation Error

**Cause:** File exceeds 10 MB limit

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body", "content_length_bytes"],
      "msg": "ensure this value is less than or equal to 10485760",
      "type": "value_error.number.not_le"
    }
  ]
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Submit Response" icon="paper-plane" href="/api-reference/custom-kpi/submit-response">
    Submit values with the uploaded attachments
  </Card>

  <Card title="Edit Values" icon="pencil" href="/api-reference/custom-kpi/edit-assignment-values">
    Update values for a campaign-assignment
  </Card>
</CardGroup>
