# WebSocket Authentication

\
To listen to private channels an authorization token is required. This is accomplished by making an HTTP request to the [Channel Authorization Endpoint](#channel-authorization-endpoint). The authorization token received should be set in the payload of the subscribe message.

### Pusher Channel Authorization

If  you are using Pusher client the authorization endpoint should be set when initializing Pusher instance, read more about setting the authorization endpoint [here](https://pusher.com/docs/channels/server_api/authorizing-users/#client-side-setting-the-authorization-endpoint).

Example of initialization of the Pusher with `channelAuthorization`

```javascript
var pusher = new Pusher("APP_KEY", {
      cluster: "eu",
      forceTLS: true,
      disableStats: true,
      enabledTransports: ["wss", "ws"],
      activityTimeout: 10000,
      channelAuthorization: {
        endpoint: `https://app.niza.io/trade/v1/broadcasting/auth`,
        transport: "ajax",
        headers: {
          "X-API-Key": "[API_KEY]",
          "X-API-Sign": "[GENERATED_SIGNATURE]",
          "Access-Control-Allow-Origin": "*",
        },
      },
    });

```

## Channel Authorization Endpoint

<mark style="color:green;">`POST`</mark> `/broadcasting/auth`

Authorize channel subscription. Request should be made to the general [Request URL](/general.md).

**Headers**

| Name         | Value                                                                    |
| ------------ | ------------------------------------------------------------------------ |
| Content-Type | `application/json`                                                       |
| X-API-Key    | API Key                                                                  |
| X-API-Sign   | The generated signature, check api [authentication](/authentication.md). |

**Body**

| Name           | Type   | Description                                     |
| -------------- | ------ | ----------------------------------------------- |
| `socket_id`    | number | The id of the currently connected WebSocket.    |
| `channel_name` | string | The private channel name you want to subscribe. |

**Response**

{% tabs %}
{% tab title="200" %}

```json
{
    "auth": "23d6f445cd259b91adf9:c35ecc2c7a8280911ff4b4818be21ee80ae9b3843f8db60d422a3f5824474ac4"
}
```

{% endtab %}

{% tab title="400" %}

```json
{
  "error": "Invalid request"
}
```

{% endtab %}

{% tab title="403" %}

```json
{
    "error": "Access Denied"
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.niza.io/websocket-api-1.0/websocket-authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
