How to livestream to Youtube using the Youtube Live Streaming API

Tosh Velaga
2 min readOct 12, 2021

--

Video from staff at Youtube in 2013 on how to use the live streaming API

If you’re building an app and you want to allow users to livestream video directly to youtube you will need to work with the Youtube Live Streaming API. It can be super daunting to use this API, because of the multiple steps required and the lack of snippets on the internet that you can use. I had some issues working with this API so I asked a question on Stack Overflow. After some trial and error and being ignored, I solved my own problem and posted the answer to my own question.

I am writing this because I couldn’t find anyone doing this with Javascript on the client and so I hope this will help others.

Let’s get started. To create a livestream first you should peruse the API documentation to get familiar with some of the terminology. To go live there are several steps: 1) authenticate the user 2) create a broadcast 3) create a stream 4) bind the stream to the broadcast 5) send your video to a server which encodes the video and sends it to youtube 6) transition the broadcast status to live.

To make it easier to visualize here are the steps with the corresponding buttons you need to click on in order. All the functions that are executed when you click the buttons can be seen below in the code.

UI with buttons corresponding to steps in going live

Note that in production there are more steps you need to consider to make this application more user friendly. For example you need to also create a step to end the broadcast on Youtube. To do this follow the documentation and transition the broadcastStatus to complete.

You might be wondering about step 5. Well that is pretty complicated. There are probably multiple ways to do this, but the way I did it is setting up a node js server with FFmpeg to encode the video and then relay the encoded video to Youtube. You can find the code for that HERE.

If you’re curious you can check out my open source project that allows users to stream to platforms like Youtube and Twitch at the same time. Here’s the code and here’s the link to take the product for a spin. Enjoy and feel free to reach out to me if you have any questions about working with the live streaming api.

--

--