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

# Collections Service

> Manage collections and their schemas

The Collections service allows you to create, read, update, and delete collections.

## Overview

```ts theme={null}
import { SnackBaseClient } from "@snackbase/sdk";

const client = new SnackBaseClient({
  baseUrl: "https://api.example.com",
});

// Access the collections service
const collections = client.collections;
```

## List Collections

```ts theme={null}
const result = await client.collections.list();
```

## Get a Collection

```ts theme={null}
const collection = await client.collections.get("collection-id");
```

## Get Collection Names

```ts theme={null}
const names = await client.collections.getNames();
```

## Create a Collection

```ts theme={null}
const collection = await client.collections.create({
  name: "posts",
  description: "Blog posts and articles",
  fields: [
    {
      name: "title",
      type: "text",
      required: true,
    },
    {
      name: "content",
      type: "text",
      required: false,
    },
  ],
});
```

<Note>
  To update the fields of a collection, you typically need to create a
  migration. See the Migrations service for more details.
</Note>

## Update a Collection

```ts theme={null}
const updated = await client.collections.update("collection-id", {
  description: "Updated description",
});
```

## Delete a Collection

```ts theme={null}
await client.collections.delete("collection-id");
```

<Warning>
  Deleting a collection permanently deletes all records within it. This
  action cannot be undone.
</Warning>

## Export Collections

Export collections to JSON format for backup or migration:

```ts theme={null}
// Export all collections
const exportData = await client.collections.export();
// exportData contains: { collections: Collection[], rules: CollectionRule[] }
```

You can also export specific collections:

```ts theme={null}
// Export specific collections by ID
const exportData = await client.collections.export({
  collection_ids: ['col-123', 'col-456']
});
```

<Note>
  Exporting collections requires superadmin authentication. The export
  includes collection schemas and rules, but not the actual records.
</Note>

## Import Collections

Import collections from a previous export:

```ts theme={null}
// Import with error strategy (fail on conflicts)
const result = await client.collections.import({
  data: exportData,
  strategy: 'error'
});
```

The `strategy` parameter determines how conflicts are handled:

* **`error`** - Fail if a collection already exists (default)
* **`skip`** - Skip existing collections, only import new ones
* **`update`** - Update existing collections with the schema from the import

```ts theme={null}
// Import with skip strategy
const result = await client.collections.import({
  data: exportData,
  strategy: 'skip'
});

// Import with update strategy
const result = await client.collections.import({
  data: exportData,
  strategy: 'update'
});
```

<Note>
  Importing collections requires superadmin authentication. The import
  result includes per-collection status and migration IDs for tracking.
</Note>

## Field Types

| Type      | Description                |
| --------- | -------------------------- |
| `text`    | Single-line text           |
| `number`  | Numeric value              |
| `boolean` | True/false value           |
| `date`    | Date/time value            |
| `select`  | Single choice from options |
| `json`    | JSON object                |

## Next Steps

* **[Records Service](/sdk/js/services/records)** - Manage records in collections
* **[Query Builder](/sdk/js/query/overview)** - Query collection data
* **[Rules](/permissions)** - Understand permission rules
