# Connect Twitch

Receive events from your Twitch channel in a macOS, Linux or Windows terminal: follows, subscriptions, bits, raids, channel point rewards and hype train levels.

## 1. Issue a key with the Twitch events scope

Before you start, check the [Source conditions](https://developers.donatty.com/en/conditions/sources.md#twitch). In the «Секретные ключи» (Secret keys) section, click «Выпустить секретный ключ» (Issue secret key). Enter a name, tick «События Twitch» (Twitch events, `twitch:read`), choose an expiration period and click «Выпустить ключ» (Issue key).

To find out which keys support this scope, see [Secret key scopes](https://developers.donatty.com/en/tokens.md#secret-key-scopes).

[Open Developers in your dashboard](https://streamer.donatty.com/developers)

Save the key in the `DONATTY_TOKEN` variable: [how to do it](https://developers.donatty.com/en/tokens.md#save-the-key-in-a-variable).

## 2. Check the source


**macOS and Linux:**

```sh
curl -s "https://api.donatty.com/v1/event-sources" -H "Authorization: Bearer $DONATTY_TOKEN"
```
**Windows (PowerShell):**

```powershell
curl.exe -s "https://api.donatty.com/v1/event-sources" -H "Authorization: Bearer $env:DONATTY_TOKEN"
```


Find the item with `"platform": "TWITCH"` in `items`. Before the first connection its status is `IDLE`: there are no stream connections yet. This is expected. No such item means the key has no `twitch:read` scope or no Twitch account is linked to your dashboard.

## 3. Connect to the twitch channel


**macOS and Linux:**

```sh
curl -N -X POST "https://api.donatty.com/v1/realtime/uni_http_stream" \
  -H "Authorization: Bearer $DONATTY_TOKEN" -H "Content-Type: application/json" -d '{"data": {"channels": ["twitch"]}}'
```
**Windows (PowerShell):**

```powershell
'{"data": {"channels": ["twitch"]}}' | curl.exe -N -X POST "https://api.donatty.com/v1/realtime/uni_http_stream" `
  -H "Authorization: Bearer $env:DONATTY_TOKEN" -H "Content-Type: application/json" --data-binary "@-"
```


In PowerShell, pipe the request body through `|` to preserve the JSON quotes when calling curl.exe.

The command reads the stream until you press Ctrl+C. In the first line, `subs` contains the `twitch:str_…` channel. For when collection starts and how long events take to arrive, see [Source conditions](https://developers.donatty.com/en/conditions/sources.md#twitch).

Event data is in `pub.data`, for example `twitch.follow` when someone follows the channel. All types and their fields are in [Twitch event formats](https://developers.donatty.com/en/structures/twitch-events.md).

## 4. Check that the source is ready

Repeat the request from step 2 in another terminal window and wait for the `READY` status. A status change also arrives in the stream as a `source.status_changed` event. For the meaning of each status, see [Source status](https://developers.donatty.com/en/sources.md#source-status).

## 5. Read a gap

After a long disconnection, use the Platform events method to catch up. For commands and steps, see the [Stream and catch-up](https://developers.donatty.com/en/examples/stream-recovery.md) example. For recovery requirements, see [History and catch-up](https://developers.donatty.com/en/realtime/recovery.md#how-to-read-the-gap). REST cannot return events that Donatty did not receive from Twitch. See [Source conditions](https://developers.donatty.com/en/conditions/sources.md#twitch).

## Next

- How to reconnect and catch up on Twitch events: [Stream and catch-up](https://developers.donatty.com/en/examples/stream-recovery.md).
- All Twitch event types and their fields: [Twitch event formats](https://developers.donatty.com/en/structures/twitch-events.md).
- The `twitch` channel, its `twitch:read` scope and other available channels: [Stream channels](https://developers.donatty.com/en/realtime/channels.md).
