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 endpoint. Here is a javascript sample code.
<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.
{
"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"
}
{
"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"
}
{
"type": "follow",
"message": [
{
"created_at": "2017-08-22 00:14:35",
"id": "170884026",
"name": "h4r5h48002",
"_id": "74a0b93e736f1f14762111f8ae34bf42"
}
],
"for": "twitch_account"
}
{
"type": "host",
"message": [
{
"name": "h4r5h48002",
"viewers": "1",
"type": "manual",
"_id": "74a0b93e736f1f14762111f8ae34bf42"
}
],
"for": "twitch_account"
}
{
"type": "bits",
"message": [
{
"id": "fc546f7d-aab8-42b2-9933-8681e9fb5eb0",
"name": "h4r5h48002",
"amount": "1",
"emotes": null,
"message": "streamlabs1",
"_id": "74a0b93e736f1f14762111f8ae34bf42"
}
],
"for": "twitch_account"
}
{
type: "raid",
message: [
{
name: "sunnyding602",
raiders: 138,
_id: "6f306e1fadf37130b5d5445b95b22436"
}
],
for: "twitch_account",
event_id: "evt_a6737f6334591943e5a04b0d792000d6"
}
{
"type": "follow",
"message": [
{
"publishedAt": "2017-08-22 00:25:57",
"id": "UCec4hVEu3ZXE8qlRtUeK0DA",
"name": "Kappa Lord",
"_id": "6af0964e548a6d93c192c9e31e0959cc"
}
],
"for": "youtube_account"
}
{
"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"
}
{
"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"
}
{
"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"
}
{
"type": "follow",
"message": [
{
"id": 1816,
"name": "Jamacanbacn",
"created_at": "2017-08-22 00:30:25",
"_id": "4fbcdd82bc9fd39467572b24ee20a766"
}
],
"for": "mixer_account"
}
{
"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.
Type | For | Event |
---|---|---|
donation | streamlabs | Donations |
follow | twitch_account | Twitch Follow |
subscription | twitch_account | Twitch Subscription |
host | twitch_account | Twitch Host |
bits | twitch_account | Twitch Bits |
raids | twitch_account | Twitch Raids |
follow | youtube_account | YouTube Subscription |
subscription | youtube_account | YouTube Subscriber |
superchat | youtube_account | YouTube Superchats |
follow | mixer_account | Mixer Follow |
subscription | mixer_account | Mixer Subscription |
host | mixer_account | Mixer Host |
Updated over 6 years ago
What’s Next
Check out how to obtain info on loyalty points for a Streamlabs account.