# Search files by metadata

URL: https://docs.sparklayer.io/developers/api/files/query-files-by-metadata
Operation: `POST /api/v1/search-files` (operationId `fileSearch`)
OpenAPI spec: https://docs.sparklayer.io/openapi/files.yaml

Returns every file whose `metadata` contains the JSON object you send (PostgreSQL JSON containment). Files that failed their post-upload checks are included and flagged with `rejected: true`. Deleted and expired files are excluded, and results are not paginated.

`POST /api/v1/search-files`

**Base URLs:** `https://app.sparklayer.io` (Live), `https://test.app.sparklayer.io` (Test)

**Authentication:** send `Authorization: Bearer <access_token>` and `Site-Id: <site id>` with every request. Get the token from [Get an access token](https://docs.sparklayer.io/developers/api/core/get-an-access-token.md); see [Authentication](https://docs.sparklayer.io/developers/authentication.md).

### Example request

```bash
curl -X POST "https://app.sparklayer.io/api/v1/search-files" \
  -H "Authorization: Bearer $SPARKLAYER_TOKEN" \
  -H "Site-Id: $SPARKLAYER_SITE_ID" \
  -H "Content-Type: application/json" \
  -d '{
  "metadata": {
    "sparklayer": {
      "forms": {
        "context": "email_template"
      }
    }
  }
}'
```

Set `SPARKLAYER_TOKEN` to an access token and `SPARKLAYER_SITE_ID` to your Site ID. For the test environment, use `https://test.app.sparklayer.io`.

### Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Site-Id` | string | Yes | Your SparkLayer Site ID, from Settings > API in the SparkLayer Dashboard. Example: `jones-climbing` |

### Request body (`application/json`) (required)

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `metadata` | any \| null | Yes | Returns all files who's metadata contains this JSON. See: https://www.postgresql.org/docs/current/datatype-json.html#JSON-CONTAINMENT |

```json
{
  "metadata": {
    "sparklayer": {
      "forms": {
        "context": "email_template"
      }
    }
  }
}
```

### Responses

#### 200: The matching files.

An array. Each item:

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `[].file_handle` | object | | |
| `[].file_handle.file_path` | string | | The path to the file, including the file name |
| `[].file_handle.id` | string | | The ID of this type |
| `[].file_handle.url` | string \| null | | URL to download/upload the file. For GET, this will be a readable link. If the file is not ready, this will be `null`. For POST/PATCH this will be a writeable link |
| `[].file_handle.required_headers` | object[] | | An array of headers you must include in the file upload/download request |
| `[].file_handle.required_headers[].name` | string | | Name of the header |
| `[].file_handle.required_headers[].value` | string | | Value of the header |
| `[].ready` | boolean | | Whether the file has been fully uploaded and verified |
| `[].mime_type` | string | | The mime type of this file |
| `[].file_size` | integer | | The size of this file in bytes |
| `[].created_at` | string (date-time) | | The date at which the file was created (RFC-3339) |
| `[].updated_at` | string (date-time) | | The date at which the file was updated (RFC-3339) |
| `[].metadata` | object | | |
| `[].rejected` | boolean | | |

```json
[
  {
    "file_handle": {
      "file_path": "folder/file.png",
      "id": "86027e2c-e853-11ee-b1d4-7c214a6ed8c7",
      "url": "https://files.sparklayer.io/siteId/siteEnv/path/to/file.png.",
      "required_headers": [
        {
          "name": "Content-Type",
          "value": "application/pdf"
        }
      ]
    },
    "ready": true,
    "mime_type": "image/png",
    "file_size": 255,
    "created_at": "2020-01-01T00:00:00+02:00",
    "updated_at": "2020-01-01T00:00:00+02:00",
    "metadata": {
      "sparklayer": {
        "forms": {
          "context": "email_template",
          "step_id": "erkjs"
        },
        "emails": {
          "attachments": [
            "86027e2c-e853-11ee-b1d4-7c214a6ed8c7"
          ]
        }
      }
    },
    "rejected": true
  }
]
```

#### Other statuses: An error. The body describes the problem: see [Errors](https://docs.sparklayer.io/developers/errors.md).

`application/problem+json`: the standard error body (below).

### Error body

Error responses with a body use this RFC 7807 problem details object. See [Errors](https://docs.sparklayer.io/developers/errors.md) for every status code and which errors to retry.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `detail` | string | | Human-readable summary of the error |
| `status` | integer | | HTTP Status code returned from API |
| `title` | string | | Machine-readable error code |
| `type` | string \| null | | |
| `errors` | object[] \| null | | A list of errors related to the call |
| `errors[].code` | string | | Machine-readable error code |
| `errors[].property` | string | | The property the error relates to |
| `errors[].message` | string | | Human readable summary of the error |

```json
{
  "detail": "Data Validation Failed",
  "status": 400,
  "title": "invalid-api-request-contents",
  "type": "https://docs.sparklayer.io/api/errors#invalid-api-request-contents",
  "errors": [
    {
      "code": "resource-not-found",
      "property": "template",
      "message": "template not found"
    }
  ]
}
```
