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

# Attaching evidence to multiple events

> Upload evidence files to multiple Mangrove events using the API.

This cookbook explains how to upload an evidence file to multiple Mangrove events using the API.

<Card title="Example Scenario">
  You have a single evidence file (e.g., a meter reading export) that needs to be attached to multiple events in your Mangrove project. Instead of uploading manually to each event, you can automate this by iterating over your event IDs and calling the API.
</Card>

### Uploading Evidence with Code

<Steps>
  <Step title="Prerequisites">
    Before starting, make sure you have:

    * A Mangrove API token with Data Collection permissions (read and write access)
    * A list of event IDs you want to attach evidence to
    * Your evidence files
    * You can use a tool like [Postman](https://www.postman.com/) to make the API calls.
  </Step>

  <Step title="Convert Evidence File to the right format">
    The API accepts evidence files as base64-encoded strings.

    Open Terminal and run:

    ```bash theme={null}
    base64 -i path/to/your/file.csv | pbcopy
    ```

    This converts your file into base64 and copies it to your clipboard.
  </Step>

  <Step title="Upload to a Single Event">
    Use the [Create evidence on event](/api-reference/evidence/create) endpoint to attach evidence to each event.

    Here's an example request to upload evidence to one event:

    ```bash curl theme={null}
    curl -X POST "https://app.gomangrove.com/api/v1/events/evt_abc123/evidence" \
      -H "Authorization: Bearer YOUR_API_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "evidences": [
          {
            "type": "file",
            "data": {
              "file": {
                "name": "meter_readings.csv",
                "type": "text/csv",
                "base64": "YOUR_BASE64_STRING"
              }
            }
          }
        ]
      }'
    ```
  </Step>

  <Step title="Upload to Multiple Events">
    To upload to multiple events, iterate over your list of event IDs. Here are examples in different languages:

    <CodeGroup>
      ```bash Shell Script theme={null}
      #!/bin/bash
      API_TOKEN="YOUR_API_TOKEN"
      FILE_BASE64=$(base64 -i evidence.csv)
      EVENT_IDS=("evt_001" "evt_002" "evt_003")

      for EVENT_ID in "${EVENT_IDS[@]}"; do
        curl -X POST "https://app.gomangrove.com/api/v1/events/${EVENT_ID}/evidence" \
          -H "Authorization: Bearer $API_TOKEN" \
          -H "Content-Type: application/json" \
          -d "{
            \"evidences\": [{
              \"type\": \"file\",
              \"data\": {
                \"file\": {
                  \"name\": \"evidence.csv\",
                  \"type\": \"text/csv\",
                  \"base64\": \"$FILE_BASE64\"
                }
              }
            }]
          }"
        echo "Uploaded to $EVENT_ID"
      done
      ```

      ```python Python theme={null}
      import requests
      import base64

      API_TOKEN = "YOUR_API_TOKEN"
      EVENT_IDS = ["evt_001", "evt_002", "evt_003"]

      # Read and encode the file
      with open("evidence.csv", "rb") as f:
          file_base64 = base64.b64encode(f.read()).decode()

      headers = {
          "Authorization": f"Bearer {API_TOKEN}",
          "Content-Type": "application/json"
      }

      for event_id in EVENT_IDS:
          response = requests.post(
              f"https://app.gomangrove.com/api/v1/events/{event_id}/evidence",
              headers=headers,
              json={
                  "evidences": [{
                      "type": "file",
                      "data": {
                          "file": {
                              "name": "evidence.csv",
                              "type": "text/csv",
                              "base64": file_base64
                          }
                      }
                  }]
              }
          )
          print(f"{event_id}: {response.status_code}")
      ```

      ```javascript Node.js theme={null}
      const fs = require('fs');

      const API_TOKEN = 'YOUR_API_TOKEN';
      const EVENT_IDS = ['evt_001', 'evt_002', 'evt_003'];

      const fileBase64 = fs.readFileSync('evidence.csv').toString('base64');

      async function uploadEvidence() {
        for (const eventId of EVENT_IDS) {
          const response = await fetch(
            `https://app.gomangrove.com/api/v1/events/${eventId}/evidence`,
            {
              method: 'POST',
              headers: {
                'Authorization': `Bearer ${API_TOKEN}`,
                'Content-Type': 'application/json'
              },
              body: JSON.stringify({
                evidences: [{
                  type: 'file',
                  data: {
                    file: {
                      name: 'evidence.csv',
                      type: 'text/csv',
                      base64: fileBase64
                    }
                  }
                }]
              })
            }
          );
          console.log(`${eventId}: ${response.status}`);
        }
      }

      uploadEvidence();
      ```
    </CodeGroup>

    <Note>
      If you're attaching the same file to multiple events, encode it once and reuse the base64 string for each request.
    </Note>
  </Step>
</Steps>

### Using Postman

You can also use Postman's Collection Runner to upload evidence to multiple events without writing code.

<Steps>
  <Step title="Create a Collection and Request">
    1. Open Postman
    2. Create a new Collection (e.g., "Mangrove Evidence Upload")
    3. Inside the collection, click **Add Request**
  </Step>

  <Step title="Configure the Request">
    **URL:**

    ```
    POST https://app.gomangrove.com/api/v1/events/{{event_id}}/evidence
    ```

    **Authorization tab:**

    * Set **Type** to `Bearer Token`
    * Paste your Mangrove API token

    **Headers tab:**

    | Key          | Value            |
    | ------------ | ---------------- |
    | Content-Type | application/json |

    **Body tab:**

    * Select **raw** and set format to **JSON**
    * Paste the following:

    ```json theme={null}
    {
      "evidences": [
        {
          "type": "file",
          "data": {
            "file": {
              "name": "{{file_name}}",
              "type": "text/csv",
              "base64": "{{file_base64}}"
            }
          }
        }
      ]
    }
    ```

    <Note>
      The `{{event_id}}`, `{{file_name}}`, and `{{file_base64}}` variables will be populated from your CSV file.
    </Note>
  </Step>

  <Step title="Convert Evidence File to Base64">
    Open Terminal (macOS) and run:

    ```bash theme={null}
    base64 -i path/to/your/file.csv | pbcopy
    ```

    This converts your file to base64 and copies it to your clipboard.
  </Step>

  <Step title="Prepare the Runner Data File">
    Create a file called `events.csv` with these columns:

    | Column       | Description                         |
    | ------------ | ----------------------------------- |
    | event\_id    | The event ID to attach evidence to  |
    | file\_name   | File name displayed in Mangrove     |
    | file\_base64 | Base64 string of your evidence file |

    ```csv Example theme={null}
    event_id,file_name,file_base64
    evt_001,evidence.csv,AAAABBBB...
    evt_002,evidence.csv,AAAABBBB...
    evt_003,evidence.csv,AAAABBBB...
    ```

    <Note>
      If attaching the same file to multiple events, reuse the same base64 value across rows.
    </Note>
  </Step>

  <Step title="Run the Collection">
    1. In Postman, open your Collection
    2. Click **Run**
    3. Upload `events.csv` as the data file
    4. Click **Run**

    Postman will send one request per row in your CSV file. The number of successful responses should match the number of rows.
  </Step>
</Steps>
