# Connect YouTube

Receive events from your YouTube channel in a macOS, Linux or Windows terminal: new subscribers, channel members, Super Chats and Super Stickers.

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

Before you start, check the [Source conditions](https://developers.donatty.com/en/conditions/sources.md#youtube). In the «Секретные ключи» (Secret keys) section, click «Выпустить секретный ключ» (Issue secret key). Enter a name, tick «События YouTube» (YouTube events, `youtube: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 items with `"platform": "YOUTUBE"` in `items`: one for each linked channel. Before the first connection their status is `IDLE`: there are no stream connections yet. This is expected. No such items means the key has no `youtube:read` scope or no YouTube channel is linked to your dashboard.

## 3. Connect to the youtube 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": ["youtube"]}}'
```
**Windows (PowerShell):**

```powershell
'{"data": {"channels": ["youtube"]}}' | 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 `youtube:str_…` channel. While the connection is open, Donatty polls YouTube for your channel. For what happens after the first poll and how long events take to arrive, see [Source conditions](https://developers.donatty.com/en/conditions/sources.md#youtube).

Event data is in `pub.data`, for example `youtube.super_chat` when a viewer sends a Super Chat. The exact amount in the viewer's currency is in `details.amount`: [`ExactMoney`](https://developers.donatty.com/en/structures.md#exactmoney). All types and their fields are in [YouTube event formats](https://developers.donatty.com/en/structures/youtube-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: polling works. `READY` does not mean an event has already arrived: a new event shows up in the stream from step 3 when it happens on the channel. A status change also arrives in the stream as a `source.status_changed` event. For the meaning of each status and `unavailableTypes`, see [Source status](https://developers.donatty.com/en/sources.md#source-status); for event types that YouTube may not provide, see [Source conditions](https://developers.donatty.com/en/conditions/sources.md#youtube).

## 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 YouTube. See [Source conditions](https://developers.donatty.com/en/conditions/sources.md#youtube).

## Next

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