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

# Create collection

Create a new collection linked to a source for instant database sync.

` POST /collections `

Create collection

Create a new collection linked to a source for instant database sync.

## Servers

- ` https://hookstream.io/v1 ` — Production

## Authentication

The operation accepts these alternatives. Schemes within one alternative are required together.

### Alternative 1

- ` apiKey `: ` apiKey `

  - Credential: ` X-API-Key ` in ` header `

  - API key (format: hs_live_<64hex> or hs_test_<64hex>)

### Alternative 2

- ` cookieAuth `: ` apiKey `

  - Credential: ` better-auth.session_token ` in ` cookie `

  - Session cookie (browser auth)

## Request body (required)

Content type: ` application/json `

Request body

- ` value `: type ` object `

  - Required fields: ` name `, ` slug `, ` source_id `, ` primary_key_path `

  - ` name `: type ` string ` — Collection name

    - example: ` "Users" `

    - minLength: ` 1 `

  - ` slug `: type ` string ` — URL-safe slug

    - example: ` "users" `

    - minLength: ` 1 `

  - ` source_id `: type ` string ` — Source ID to sync from

    - minLength: ` 1 `

  - ` primary_key_path `: type ` string ` — JSONPath to the record primary key

    - example: ` "$.id" `

    - minLength: ` 1 `

  - ` event_type_field `: type ` string ` — Field used to determine event type

  - ` description `: type ` string ` — Optional description

## Responses

### ` 201 ` — Collection created

Content type: ` application/json `

- ` value `: type ` object `

  - Required fields: ` collection `

  - ` collection `: type ` object `; nullable

    - Required fields: ` id `, ` org_id `, ` env_id `, ` name `, ` slug `, ` source_id `, ` primary_key_path `, ` event_type_field `, ` description `, ` status `, ` record_count `, ` last_sync_at `, ` created_at `, ` updated_at `, ` backfill_status `, ` backfill_progress `, ` backfill_total `, ` backfill_cursor `, ` backfill_started_at `, ` backfill_completed_at `, ` backfill_error `, ` source_name `

    - ` id `: type ` string `

    - ` org_id `: type ` string `

    - ` env_id `: type ` string `

    - ` name `: type ` string `

    - ` slug `: type ` string `

    - ` source_id `: type ` string `

    - ` primary_key_path `: type ` string `

    - ` event_type_field `: type ` string `; nullable

    - ` description `: type ` string `; nullable

    - ` status `: type ` string `

    - ` record_count `: type ` integer `

    - ` last_sync_at `: type ` string `; nullable

    - ` created_at `: type ` string `

    - ` updated_at `: type ` string `

    - ` backfill_status `: type ` string `; nullable

    - ` backfill_progress `: type ` integer `; nullable

    - ` backfill_total `: type ` integer `; nullable

    - ` backfill_cursor `: type ` string `; nullable

    - ` backfill_started_at `: type ` string `; nullable

    - ` backfill_completed_at `: type ` string `; nullable

    - ` backfill_error `: type ` string `; nullable

    - ` source_name `: type ` string `; nullable

### ` 400 ` — Bad request

Content type: ` application/json `

- ` value `: type ` object `

  - Required fields: ` error `

  - ` error `: type ` string ` — Error message

### ` 401 ` — Unauthorized

Content type: ` application/json `

- ` value `: type ` object `

  - Required fields: ` error `

  - ` error `: type ` string ` — Error message

### ` 404 ` — Source not found

Content type: ` application/json `

- ` value `: type ` object `

  - Required fields: ` error `

  - ` error `: type ` string ` — Error message

## Request examples

### cURL

```curl
curl -X POST 'https://hookstream.io/v1/collections' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Users",
  "slug": "users",
  "source_id": "string",
  "primary_key_path": "$.id",
  "event_type_field": "string",
  "description": "string"
}'
```

### JavaScript

```javascript
const response = await fetch('https://hookstream.io/v1/collections', {
  method: 'POST',
  headers: {
      "Authorization": "Bearer YOUR_API_TOKEN",
      "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "name": "Users",
    "slug": "users",
    "source_id": "string",
    "primary_key_path": "$.id",
    "event_type_field": "string",
    "description": "string"
  })
});

const data = await response.json();
console.log(data);
```

### Python

```python
import requests

headers = {
    'Authorization': 'Bearer YOUR_API_TOKEN'
}

response = requests.post('https://hookstream.io/v1/collections', headers=headers, json={
  "name": "Users",
  "slug": "users",
  "source_id": "string",
  "primary_key_path": "$.id",
  "event_type_field": "string",
  "description": "string"
})
print(response.json())
```

### Go

```go
package main

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

func main() {
	body := strings.NewReader(`{
  "name": "Users",
  "slug": "users",
  "source_id": "string",
  "primary_key_path": "$.id",
  "event_type_field": "string",
  "description": "string"
}`)
	req, _ := http.NewRequest("POST", "https://hookstream.io/v1/collections", body)
	req.Header.Set("Authorization", "Bearer YOUR_API_TOKEN")
	req.Header.Set("Content-Type", "application/json")

	resp, _ := http.DefaultClient.Do(req)
	defer resp.Body.Close()
	result, _ := io.ReadAll(resp.Body)
	fmt.Println(string(result))
}
```