Getting Started
This guide will help you get started with YouTubeSessionGenerator by walking you through the setup and usage of the library to generate a trusted session for the internal YouTube API (InnerTube).
Note
If you haven't installed the library yet, head over to the Installation page first.
1. Create an instance
To start generating session tokens like Visitor Data, Proof of Origin Tokens or Rollout Tokens, you'll first need to initialize an instance of YouTubeSessionCreator with a configuration:
using YouTubeSessionGenerator;
YouTubeSessionConfig config = new()
{
JsEnvironment = myCustomJsEnvironment, // Required when generating Proof of Origin Tokens
HttpClient = myCustomHttpClient, // Optional: Provide your own HttpClient
Logger = myCustomLogger // Optional: Enable logging
};
YouTubeSessionCreator creator = new(config);
Caution
If you're passing a JsEnvironment
, you are responsible for disposing it! For example, the built-in NodeEnvironment
spawns a subprocess that won't be closed until disposed.
Use a using
statement or manually call .Dispose()
in a try/finally
block when done.
2. Generate Session Tokens
Once we have an instance of YouTubeSessionCreator, we can start generating the session tokens:
Generate Visitor Data
string visitorData = await creator.VisitorDataAsync();
Generate Proof of Origin Token
// Requires a JavaScript environment!
string poToken = await creator.ProofOfOriginTokenAsync(visitorData);
Generate Rollout Token
string rolloutToken = await creator.RolloutTokenAsync();
3. Use the Tokens
Once you have the tokens, you can use them in your HTTP requests to YouTube's InnerTube API:
{
"context": {
"client": {
"visitorData": "<VISITOR_DATA>",
"rolloutToken": "<ROLLOUT_TOKEN>",
...
}
},
"serviceIntegrityDimensions": {
"poToken": "<PROOF_OF_ORIGIN_TOKEN>"
},
...
}
Note
This structure is commonly used for requests to endpoints like browse
, player
, or next
. Some fields may vary depending on the endpoint.
What's Next?
- Learn about all available config options in the Configuration guide
- Dive deeper into how the tokens work
- Explore the library reference