Modify Channel Details
Learn to update the information of a channel using the Twitch API.
We'll cover the following
Update channel information
We might end up in situations where we have to update the information displayed on our channel page. The Twitch API allows us to do so using the channels endpoint through a simple PATCH request.
All PATCH requests to update a channel's information need to be authorized with a user access token that has the channel:manage:broadcast
scope.
Request parameters
To make a successful request, we need to provide the channel ID as a required query parameter. We can also pass some optional body parameters. The table below gives an overview of these parameters:
Parameter | Parameter Type | Type | Category | Description |
| Query | String | Required | This is the ID of the channel for which we want to update the details. |
| Body | String | Optional | This is the ID of the game currently being played on the stream. We can clear this value by passing |
| Body | String | Optional | This is an ISO-639-1 two-letter code for the language in which the channel broadcasts. Examples of its accepted values are |
| Body | String | Optional | This is the title of the last or current stream. This cannot be an empty string. |
| Body | Integer | Optional | This is the stream delay in seconds. Since it's a Twitch Partner feature, normal users can't update this value. |
Although all body parameters are listed as optional parameters, we must provide at least one of these parameters for the request to be valid.
Let's make a sample request to this endpoint and authorize ourselves with a user access token that has the required scope. We can also change the parameters in, or add more parameters to, the payload
object starting on line 16 to values of our liking.
Note: If your token has expired, return to this lesson and follow the steps to generate a new one.
import fetch from "node-fetch";const endpointUrl = new URL("https://api.twitch.tv/helix/channels");const headerParameters = {Authorization: "Bearer {{USER_ACCESS_TOKEN}}","Client-Id": "{{CLIENT_ID}}","Content-Type": "application/json",};const queryParameters = new URLSearchParams({broadcaster_id: "{{USER_ID}}",});// Defining the parameters to be changed as body parametersconst bodyParameters = JSON.stringify({broadcaster_language: "other",game_id: "509658", // Just Chatting});const options = {method: "PATCH",headers: headerParameters,body: bodyParameters,};async function updateChannelDetails() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}updateChannelDetails();
In the code above, we perform the following:
Lines 5–9: We define the headers for the request, specifying the content type as
application/json
.Lines 11–13: We define the query parameters for the call using the
broadcaster_id
parameter.Lines 16–19: We define the body parameters using the
broadcaster_language
andgame_id
parameters.Lines 21–25: We set the options for the API call, specifying the HTTP request type as PATCH and providing the request headers and body.
Lines 27–35: We define a function called
updateChannelDetails
to make a call to the channels endpoint.Lines 28–32: Within a
try
block, we provide the query parameters, make a call to the endpoint, and print the response from the API.Lines 32–34: We catch any errors or exceptions within a
catch
block and print them to the console.
Line 37: We invoke the
updateChannelDetails
function.
Response fields
The response from this request only contains an HTTP status code. If the API responds with 204
, the request was successful. If the request fails, it returns an error code instead. The table below gives a brief summary of these status codes:
Status Code | Description |
| This is a success code indicating the details were successfully updated. |
| This is an error code indicating that the request was invalid due to missing or invalid parameters. This code is also returned if a non-partner user tries to update the |
| This is an error code indicating that the request failed due to server-side issues. |
Now that we've updated our channel details using the API, let's fetch our channel details once more to see whether the changes we made are reflected or not.
import fetch from "node-fetch";const endpointUrl = new URL("https://api.twitch.tv/helix/channels");const headerParameters = {Authorization: "Bearer {{APP_ACCESS_TOKEN}}","Client-Id": "{{CLIENT_ID}}",};const queryParameters = new URLSearchParams({broadcaster_id: "{{USER_ID}}",});const options = {method: "GET",headers: headerParameters,};async function fetchChannelDetails() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}fetchChannelDetails();
If all goes smoothly, the changes we made in the previous request should reflect in response to this request.