# Collection routes: export, query

URL: /code/api/collection/verbs · Id: `page:code/api/collection/verbs`

> The Collection Verbs export, query as api.sb routes, with inputs, result, refusals and an example.

Reference for [Collection](/business/nouns/collection), one group of [Collection routes](/code/api/collection).

### Collection.export
Export writes a Collection’s records to a file or the Startup’s warehouse, once or on a schedule. [Verb page](/business/nouns/collection/export?interface=api).

```text
POST /{namespace}/collections/{collection}/export
```

Path: `collection`. Body, as JSON: the other inputs. Emits [`Collection.exported`](/business/events/Collection.exported).

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `collection` | [Collection](/business/nouns/collection) | Yes | The Collection to export. |
| `to` | `string` | Yes | A file format, or the warehouse Integration that receives it. |
| `cadence` | `string` | No | How often it repeats; omitted, it runs once. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `file` | [File](/business/nouns/file) | No | The exported File, for a one-time export. |
| `collection` | [Collection](/business/nouns/collection) | Yes | The Collection exported. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X POST https://api.sb/leaseline.app/collections/collection_txTK8QorKn/export \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"to":"Example to"}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "file": {
    "$id": "https://api.sb/leaseline.app/files/file_V1AuhyCTsH",
    "$type": "File",
    "contentType": "Example content type",
    "size": 1
  },
  "collection": {
    "$id": "https://api.sb/leaseline.app/collections/collection_txTK8QorKn",
    "$type": "Collection",
    "id": "Example id",
    "type": [
      "noun_v0akLUrXCN"
    ]
  },
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "Collection.export",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```

### Collection.query
Query reads any Nouns the caller’s Permissions allow, by SQL or a Relationship path, across every System in one call. [Verb page](/business/nouns/collection/query?interface=api).

```text
POST /{namespace}/collections/query
```

Body, as JSON: the inputs. Emits [`Collection.queried`](/business/events/Collection.queried).

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `query` | `text` | Yes | SQL, or a Relationship path such as `customer.subscriptions.invoices`. |
| `save` | `boolean` | No | True saves it as a Collection defined by the query. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `rows` | `json` | Yes | The matching records. |
| `collection` | [Collection](/business/nouns/collection) | Yes | The Collection the query read. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X POST https://api.sb/leaseline.app/collections/query \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query":"Example query"}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "rows": {},
  "collection": {
    "$id": "https://api.sb/leaseline.app/collections/collection_txTK8QorKn",
    "$type": "Collection",
    "id": "Example id",
    "type": [
      "noun_v0akLUrXCN"
    ]
  },
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "Collection.query",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```