Change Channel Title and Game

So, I am trying to change the channel title and game from my tmi.js application. I have tried doing everything from this:

function putItem()
{
	client.api({
		url: "https://api.twitch.tv/kraken/channels/[My Channel Id]",
		method: "PUT",
		headers: {
			"Accept": "application/vnd.twitchtv.v5+json",
			"Authorization": "OAuth [My Access Token]",
			"Client-ID": "[My Client ID]",
			"Content-Type": "application/json"
		},
		body: {"channel": {"status": "The Finalest of Fantasies", "game": "Final Fantasy XV", "channel_feed_enabled": true}},
		json: true
		}, function(err, res, body) {
			console.log(body);
	});
}

To this:

function putItem()
{
	client.api({
		url: "https://api.twitch.tv/kraken/channels/[My Channel Id]",
		method: "PUT",
		headers: {
			"Accept": "application/vnd.twitchtv.v5+json",
			"Authorization": "OAuth [My Access Token]",
			"Client-ID": "[My Client ID]"
		},
		body: "channel[status]=Playing+cool+new+game!&channel[game]=Diablo"
		}, function(err, res, body) {
			console.log(body);
	});
}

And yes, [My Channel ID], [My Client ID] were not placeholders, I censored them out for this post.

My Twitch API request still responds with data, but the data was not changed the way I wanted it. It was returning what I previously had, and it wasn’t being changed on my channel, either. It did respond with the previous status and game data, which was not Diablo or Final Fantasy. How do I correctly use this API?

Your second one sends a form-encoded body but says it’s json in the content-type header. You’re also missing a comma in both examples after the headers object, before body.

Are you doing this in browser or node?

Sorry, I missed those things when I was altering the syntax for the post. My syntax was correct in my app. I am using node.js and Electron, which means it’s technically in a browser but I can do the same things that node.js can (like require, no browser security/CORS protection, etc).

client.api forces the method to be “GET” (Reference) which I had committed a fix for (12d7e82 & b954cac) but it got skipped from merging to master and I don’t have access on NPM to update the module, only Schmoopiie can. Use the request library directly instead:

let { channelID, authToken, clientID } = require('../config'); // Or whatever

function putItem(status, game) {
	return request({
		baseUrl: 'https://api.twitch.tv/kraken/',
		url: 'channels/' + channelID,
		method: 'PUT',
		headers: {
			Accept: 'application/vnd.twitchtv.v5+json',
			Authorization: 'OAuth ' + authToken,
			'Client-ID': clientID
		},
		json: true,
		body: {
			channel: { status, game }
		}
	}, (err, { statusCode }, body) => {
		if(err) {
			console.log(err);
		}
		else if(statusCode !== 200) {
			console.log({ statusCode, body });
		}
		else {
			console.log(body);
		}
	});
}

putItem('Hey, we\'re playing games up in here!', 'Destiny'); // Or whatever
2 Likes

:smiley: thanks been trying to figure this out for two days, I initially started to use request but was told not to by my friend cause client.api was the same but apparently the method isn’t.

Yeah sorry about that. ¯\_(ツ)_/¯ Schmoopiie’s not very available. I definitely would recommend request anyways.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.