---
updatedAt: 2025-06-05T20:01:55.000Z
agentTools:
  projectIndex: https://dev.streamlabs.com/llms.txt
---

# Socket API

Socket API is the fastest we have made so far. Socket API allows you to get access to real time data for any get requests (see references for all the types of requests you can make). This is our recommended method for the API usage.

## 1. Fetch the token

A socket token can be fetched using [/socket/token](https://dev.streamlabs.com/reference/sockettoken) endpoint. Here is a javascript sample code.

```javascript fetch_token.js
<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js' type='text/javascript'></script>
<script type = 'text/javascript'>
  const socketToken = ''; //Socket token from /socket/token end point
  
  //Connect to socket
const streamlabs = io(`https://sockets.streamlabs.com?token=${socketToken}`, {transports: ['websocket']});
  
  //Perform Action on event
  streamlabs.on('event', (eventData) => {
    if (!eventData.for && eventData.type === 'donation') {
      //code to handle donation events
      console.log(eventData.message);
    }
    if (eventData.for === 'twitch_account') {
      switch(eventData.type) {
        case 'follow':
          //code to handle follow events
          console.log(eventData.message);
          break;
        case 'subscription':
          //code to handle subscription events
          console.log(eventData.message);
          break;
        default:
          //default case
          console.log(eventData.message);
      }
    }
  });
</script>
```

Please note that in the execution of the above code, the returned value will always be an `array type`. Below are the JSON type responses that will be returned upon triggering specific events.

```json Donations
{
  "type": "donation",
  "message": [
    {
      "id": 96164121,
      "name": "test",
      "amount": "13.37",
      "formatted_amount": "$13.37",
      "formattedAmount": "$13.37",
      "message": "test donation",
      "currency": "USD",
      "emotes": null,
      "iconClassName": "user",
      "to": {
        "name": "Sai Harsha Maddela"
      },
      "from": "test",
      "from_user_id": null,
      "_id": "0820c9d5bafd768c9843f5e35c885e71"
    }
  ],
  "event_id": "evt_17e5f4dc6888767ed9799f78dfa2cabc"
}
```
```json Twitch Subscriptions
{
  "type": "subscription",
  "message": [
    {
      "name": "kevin",
      "months": 7,
      "message": "sub message",
      "emotes": null,
      "sub_plan": "1000",
      "sub_plan_name": "Sample Plan name",
      "sub_type": "resub",
      "_id": "a226a8c223634afdc368439c1da7aaba"
    }
  ],
  "for": "twitch_account"
}
```
```json Twitch Follow
{
  "type": "follow",
  "message": [
    {
      "created_at": "2017-08-22 00:14:35",
      "id": "170884026",
      "name": "h4r5h48002",
      "_id": "74a0b93e736f1f14762111f8ae34bf42"
    }
  ],
  "for": "twitch_account"
}
```
```json Twitch Host
{
  "type": "host",
  "message": [
    {
      "name": "h4r5h48002",
      "viewers": "1",
      "type": "manual",
      "_id": "74a0b93e736f1f14762111f8ae34bf42"
    }
  ],
  "for": "twitch_account"
}
```
```json Twitch Bits
{
  "type": "bits",
  "message": [
    {
      "id": "fc546f7d-aab8-42b2-9933-8681e9fb5eb0",
      "name": "h4r5h48002",
      "amount": "1",
      "emotes": null,
      "message": "streamlabs1",
      "_id": "74a0b93e736f1f14762111f8ae34bf42"
    }
  ],
  "for": "twitch_account"
}
```
```json Twitch Raids
{
  type: "raid", 
  message: [
    {
      name: "sunnyding602",  
      raiders: 138, 
      _id: "6f306e1fadf37130b5d5445b95b22436"
    }
           ], 
  for: "twitch_account", 
  event_id: "evt_a6737f6334591943e5a04b0d792000d6"
}
```
```json YouTube Subscriptions
{
  "type": "follow",
  "message": [
    {
      "publishedAt": "2017-08-22 00:25:57",
      "id": "UCec4hVEu3ZXE8qlRtUeK0DA",
      "name": "Kappa Lord",
      "_id": "6af0964e548a6d93c192c9e31e0959cc"
    }
  ],
  "for": "youtube_account"
}
```
```json YouTube Sponsor
{
  "type": "subscription",
  "message": [
    {
      "sponsorSince": "2017-08-22 00:45:48",
      "id": "UCec4hVEu3ZXE8qlRtUeK0DA",
      "name": "Kappa Lord",
      "channelUrl": "http://www.youtube.com/channel/UCec4hVEu3ZXE8qlRtUeK0DA",
      "months": 3,
      "_id": "33d396edab6e2e2e8b37a7b6f1b60b82"
    }
  ],
  "for": "youtube_account"
}
```
```json YouTube Superchats
{
  "type": "superchat",
  "message": [
    {
      "id": "LCC.Cg8KDQoLWmpUemdzdU1vM2sSHAoaQ05PcW01UzJ3dElDRll2WkhBb2RLd0lBOWc0.028141892513880107",
      "channelId": "UCec4hVEu3ZXE8qlRtUeK0DA",
      "channelUrl": "http://www.youtube.com/channel/UCec4hVEu3ZXE8qlRtUeK0DA",
      "name": "Kappa Lord",
      "comment": "love the stream",
      "amount": "2000000",
      "currency": "USD",
      "displayString": "$2.00",
      "messageType": 2,
      "createdAt": "2017-08-22 00:51:57",
      "_id": "ad46c7e468331c5e39373e05876fa17e"
    }
  ],
  "for": "youtube_account"
}
```
```json Mixer Subscriptions
{
  "type": "subscription",
  "message": [
    {
      "id": "123456",
      "name": "h4r5h4",
      "months": 2,
      "message": null,
      "emotes": null,
      "since": "2017-08-22 00:36:59",
      "_id": "68f034214d68dce22431801b940d2b90"
    }
  ],
  "for": "mixer_account"
}
```
```json Mixer Follow
{
  "type": "follow",
  "message": [
    {
      "id": 1816,
      "name": "Jamacanbacn",
      "created_at": "2017-08-22 00:30:25",
      "_id": "4fbcdd82bc9fd39467572b24ee20a766"
    }
  ],
  "for": "mixer_account"
}
```
```json Mixer Host
{
  "type": "host",
  "message": [
    {
      "name": "h4r5h47",
      "viewers": "5",
      "type": "manual",
      "_id": "4ee5fcf7e34128f4f07bb978c9e35fd4"
    }
  ],
  "for": "mixer_account"
}
```

Below are the `type` and `for` values for different events that were described in the above samples.

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Type
      </th>

      <th>
        For
      </th>

      <th>
        Event
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        donation
      </td>

      <td>
        streamlabs
      </td>

      <td>
        Donations
      </td>
    </tr>

    <tr>
      <td>
        follow
      </td>

      <td>
        twitch\_account
      </td>

      <td>
        Twitch Follow
      </td>
    </tr>

    <tr>
      <td>
        subscription
      </td>

      <td>
        twitch\_account
      </td>

      <td>
        Twitch Subscription
      </td>
    </tr>

    <tr>
      <td>
        host
      </td>

      <td>
        twitch\_account
      </td>

      <td>
        Twitch Host
      </td>
    </tr>

    <tr>
      <td>
        bits
      </td>

      <td>
        twitch\_account
      </td>

      <td>
        Twitch Bits
      </td>
    </tr>

    <tr>
      <td>
        raids
      </td>

      <td>
        twitch\_account
      </td>

      <td>
        Twitch Raids
      </td>
    </tr>

    <tr>
      <td>
        follow
      </td>

      <td>
        youtube\_account
      </td>

      <td>
        YouTube Subscription
      </td>
    </tr>

    <tr>
      <td>
        subscription
      </td>

      <td>
        youtube\_account
      </td>

      <td>
        YouTube Subscriber
      </td>
    </tr>

    <tr>
      <td>
        superchat
      </td>

      <td>
        youtube\_account
      </td>

      <td>
        YouTube Superchats
      </td>
    </tr>

    <tr>
      <td>
        follow
      </td>

      <td>
        mixer\_account
      </td>

      <td>
        Mixer Follow
      </td>
    </tr>

    <tr>
      <td>
        subscription
      </td>

      <td>
        mixer\_account
      </td>

      <td>
        Mixer Subscription
      </td>
    </tr>

    <tr>
      <td>
        host
      </td>

      <td>
        mixer\_account
      </td>

      <td>
        Mixer Host
      </td>
    </tr>
  </tbody>
</Table>