> ## 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 subscription

Subscribe a destination to a topic with optional filters and transforms.

` POST /topics/{id}/subscriptions `

Create subscription

Subscribe a destination to a topic with optional filters and transforms.

## 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)

## Parameters

### ` id ` (path, required)

Resource ID (32-char hex)

- ` value `: type ` string ` — Resource ID (32-char hex)

  - example: ` "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6" `

  - minLength: ` 1 `

Example:

```json
"a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"
```

## Request body (required)

Content type: ` application/json `

Request body

- ` value `: type ` object `

  - Required fields: ` destination_id `

  - ` destination_id `: type ` string ` — Destination ID to subscribe

    - minLength: ` 1 `

  - ` filter_rules `: type ` array ` — Optional filter rules for this subscription

    - ` array item `: type ` object `

  - ` transform_expression `: type ` string ` — Optional JSONata transform expression

## Responses

### ` 201 ` — Subscription created

Content type: ` application/json `

- ` value `: type ` object `

  - Required fields: ` subscription `

  - ` subscription `: type ` unknown `

    - ` allOf constraint 1 `: type ` object `

      - Required fields: ` id `, ` org_id `, ` env_id `, ` topic_id `, ` destination_id `, ` filter_rules `, ` transform_expression `, ` enabled `, ` created_at `, ` destination_name `

      - ` id `: type ` string `

      - ` org_id `: type ` string `

      - ` env_id `: type ` string `

      - ` topic_id `: type ` string `

      - ` destination_id `: type ` string `

      - ` filter_rules `: type ` string `; nullable

      - ` transform_expression `: type ` string `; nullable

      - ` enabled `: type ` integer `

      - ` created_at `: type ` string `

      - ` destination_name `: type ` string `; nullable

    - ` allOf constraint 2 `: type ` unknown `; 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 ` — Not found

Content type: ` application/json `

- ` value `: type ` object `

  - Required fields: ` error `

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

### ` 409 ` — Already subscribed

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/topics/a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6/subscriptions' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "destination_id": "string",
  "filter_rules": [
    {}
  ],
  "transform_expression": "string"
}'
```

### JavaScript

```javascript
const response = await fetch('https://hookstream.io/v1/topics/a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6/subscriptions', {
  method: 'POST',
  headers: {
      "Authorization": "Bearer YOUR_API_TOKEN",
      "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "destination_id": "string",
    "filter_rules": [
      {}
    ],
    "transform_expression": "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/topics/a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6/subscriptions', headers=headers, json={
  "destination_id": "string",
  "filter_rules": [
    {}
  ],
  "transform_expression": "string"
})
print(response.json())
```

### Go

```go
package main

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

func main() {
	body := strings.NewReader(`{
  "destination_id": "string",
  "filter_rules": [
    {}
  ],
  "transform_expression": "string"
}`)
	req, _ := http.NewRequest("POST", "https://hookstream.io/v1/topics/a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6/subscriptions", 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))
}
```