youtubetranscript-net
0.1.1
dotnet add package youtubetranscript-net --version 0.1.1
NuGet\Install-Package youtubetranscript-net -Version 0.1.1
<PackageReference Include="youtubetranscript-net" Version="0.1.1" />
<PackageVersion Include="youtubetranscript-net" Version="0.1.1" />
<PackageReference Include="youtubetranscript-net" />
paket add youtubetranscript-net --version 0.1.1
#r "nuget: youtubetranscript-net, 0.1.1"
#:package youtubetranscript-net@0.1.1
#addin nuget:?package=youtubetranscript-net&version=0.1.1
#tool nuget:?package=youtubetranscript-net&version=0.1.1
youtubetranscript-net (.NET)
The official .NET client for the YouTubeTranscript.net API: YouTube transcripts with timestamps, your transcript library, batches and credits.
- .NET 8 or newer, nullable annotations on.
- No third-party dependencies: only
HttpClientandSystem.Text.Json. - Every call is
async, takes aCancellationTokenand ends inAsync.
Install
dotnet add package youtubetranscript-net
Create an API key at https://youtubetranscript.net/account/api-keys. API calls spend the same credits as the website.
Quick start
using YouTubeTranscriptNet;
// Reads the key from the YTT_API_KEY environment variable.
using var client = new YouTubeTranscriptClient();
var result = await client.Transcripts.GetAsync("https://www.youtube.com/watch?v=jNQXAC9IVRw");
Console.WriteLine(result.Transcript!.Title);
foreach (var segment in result.Transcript.Segments)
{
Console.WriteLine($"[{segment.Start:0.0}s] {segment.Text}");
}
Pick a language, or get plain text:
var withLanguage = await client.Transcripts.GetAsync("jNQXAC9IVRw", new GetOptions { Language = "en" });
var text = await client.Transcripts.GetAsync("jNQXAC9IVRw", new GetOptions { Format = "text" });
Console.WriteLine(text.Text);
video is a watch, youtu.be or Shorts link, or an 11-character video id. The SDK sends it to the server as is.
Client
var client = new YouTubeTranscriptClient(new YouTubeTranscriptClientOptions
{
ApiKey = "ytt_...", // default: env YTT_API_KEY
BaseUrl = "https://api.youtubetranscript.net", // default: env YTT_BASE_URL, then this value
Timeout = TimeSpan.FromSeconds(60), // one HTTP request
MaxRetries = 2, // see "Retries"
});
| Option | Default | Meaning |
|---|---|---|
ApiKey |
env YTT_API_KEY |
Without a key the constructor throws API_KEY_REQUIRED and sends nothing. |
BaseUrl |
env YTT_BASE_URL, then https://api.youtubetranscript.net |
A trailing / is removed. |
Timeout |
60 s | Timeout of a single HTTP request. |
MaxRetries |
2 | Automatic retries, see below. |
Pass your own HttpClient as the second constructor argument for DI, proxies or tests. The SDK sets headers per request, never touches the HttpClient's defaults, and does not dispose it:
services.AddHttpClient<YouTubeTranscriptClient>(); // or:
var client = new YouTubeTranscriptClient(options, httpClientFromFactory);
Every request carries Authorization: Bearer <key>, User-Agent: youtubetranscript-net-csharp/0.1.1 and Accept: application/json.
Methods
client.Transcripts
| Method | HTTP | Returns |
|---|---|---|
GetAsync(video, GetOptions?) |
POST /v1/transcript, then polls GET /v1/transcript/job/{id} if needed |
TranscriptResult (always success) |
SubmitAsync(video, SubmitOptions?) |
POST /v1/transcript once |
TranscriptResult: finished, or a job (pending / processing) with JobId |
JobAsync(jobId) |
GET /v1/transcript/job/{id} |
TranscriptResult as the server answers |
InfoAsync(video) |
GET /v1/transcript/info |
VideoInfo (title, channel, caption languages). Free. |
ListAsync(ListOptions?) |
GET /v1/library/transcripts |
TranscriptList (Transcripts, Total, Page, PageSize, Langs) |
DownloadAsync(videoId, format = "txt", language?) |
GET /v1/library/transcripts/youtube/{id}/download |
string. format: txt, srt, vtt, json, md |
DeleteAsync(videoId, language?) |
DELETE /v1/library/transcripts/youtube/{id} |
RemovedResult (Removed) |
GetOptions / SubmitOptions (shared fields): Language, Format (json or text), IncludeTimestamp, SendMetadata, ConfirmCredits, IdempotencyKey. GetOptions adds Timeout (default 300 s) and PollInterval (default 2 s); SubmitOptions adds WaitMs.
GetAsync asks the server to hold the request for up to 10 s (waitMs: 10000). If the transcript is ready, you get it at once. If the server answers 202, the SDK polls the job every PollInterval until it succeeds, fails, or Timeout passes (TIMEOUT, with JobId on the exception).
TranscriptResult: Status, JobId, Language, Format, Cached, LengthSeconds, LengthText, Transcript (Platform, ExternalId, Lang, Origin, Title, Channel, DurationSec, Segments with Text / Start / Duration), Text (when Format is text), Metadata, and for jobs Stage and Error. Raw is the untouched JSON (JsonElement) for fields the SDK does not model; unknown fields never cause an error.
// Start a job and come back later.
var job = await client.Transcripts.SubmitAsync("jNQXAC9IVRw", new SubmitOptions { WaitMs = 0 });
if (!job.IsSuccess)
{
var later = await client.Transcripts.JobAsync(job.JobId!);
}
var info = await client.Transcripts.InfoAsync("https://youtu.be/jNQXAC9IVRw");
var srt = await client.Transcripts.DownloadAsync("jNQXAC9IVRw", "srt");
var page = await client.Transcripts.ListAsync(new ListOptions { Q = "zoo", PageSize = 20 });
await client.Transcripts.DeleteAsync("jNQXAC9IVRw");
client.Batches
| Method | HTTP | Returns |
|---|---|---|
CreateAsync(CreateBatchOptions) |
POST /v1/batch |
Batch |
GetAsync(batchId, maxResults?, pageToken?) |
GET /v1/batch/{id} |
BatchPage (Batch, Items, NextPageToken) |
WaitAsync(batchId, WaitOptions?) |
polls GetAsync |
the last BatchPage, once Batch.Status is completed or failed |
ExportAsync(batchId, ExportOptions?) |
GET /v1/batch/{id}/export |
byte[] (a zip by default) |
RetryAsync(batchId) |
POST /v1/batch/{id}/retry |
BatchRetryResult (Batch, Retried, Credits) |
DeleteAsync(batchId) |
DELETE /v1/library/batches/{id} |
RemovedResult |
CreateBatchOptions takes exactly one of Items (video links), Playlist or Channel, plus Language, Limit, Name. Giving none or several throws INVALID_REQUEST before any request is sent. WaitOptions: Timeout (default 1800 s), PollInterval (default 3 s).
var batch = await client.Batches.CreateAsync(new CreateBatchOptions
{
Items = new[] { "https://www.youtube.com/watch?v=jNQXAC9IVRw", "https://youtu.be/9bZkp7q19f0" },
});
var done = await client.Batches.WaitAsync(batch.Id);
Console.WriteLine($"{done.Batch.Status}: {done.Batch.Counts.Success}/{done.Batch.Counts.Total}");
await File.WriteAllBytesAsync("batch.zip", await client.Batches.ExportAsync(batch.Id));
await client.Batches.DeleteAsync(batch.Id);
client.Account
UsageAsync() returns Usage: Credits (Balance, FreeToday, Available, HasPurchased), Batch.ItemsPerJob, Limits.
var usage = await client.Account.UsageAsync();
Console.WriteLine(usage.Credits.Available);
Error handling
Every failure is a YouTubeTranscriptException:
| Property | Meaning |
|---|---|
Status |
HTTP status. 0 for errors raised locally (no response). |
Code |
The server's error.code (INVALID_VIDEO, NO_TRANSCRIPT, INSUFFICIENT_CREDITS, RATE_LIMITED, ...). A non-JSON response gives HTTP_<status>. Local codes: NETWORK_ERROR, TIMEOUT, API_KEY_REQUIRED, INVALID_REQUEST, TRANSCRIPT_FAILED, INVALID_RESPONSE. |
Message |
Human readable message. |
RequestId |
The X-Request-Id response header. Quote it to support. |
RetryAfter |
Seconds from the Retry-After header, or null. |
Details |
The server's whole error object as a JsonElement? (availableLanguages, creditsRequired, ...). |
Retryable |
true for 429, 5xx and NETWORK_ERROR. |
JobId |
The transcript job, when known (failed jobs, polling TIMEOUT). |
try
{
await client.Transcripts.GetAsync("https://www.youtube.com/watch?v=xxxxxxxxxxx");
}
catch (YouTubeTranscriptException ex) when (ex.Code == "INVALID_VIDEO")
{
Console.WriteLine($"{ex.Status} {ex.Code}: {ex.Message} (request {ex.RequestId})");
}
catch (YouTubeTranscriptException ex) when (ex.Code == "LANGUAGE_NOT_AVAILABLE")
{
Console.WriteLine(ex.Details!.Value.GetProperty("availableLanguages"));
}
If a transcript job ends as failed, GetAsync throws with the job's error.code (or TRANSCRIPT_FAILED when it has none). Cancelling through your CancellationToken throws the usual OperationCanceledException, not a YouTubeTranscriptException.
Retries
The SDK retries by itself, at most MaxRetries times (default 2), and only when the failure is Retryable (429, 5xx, NETWORK_ERROR):
| Call | Retried? |
|---|---|
All GET calls, and DELETE |
Yes |
POST /v1/transcript (GetAsync, SubmitAsync) |
Yes, because it carries an Idempotency-Key (a fresh UUID v4 per call). Every retry sends the same key, so the server never starts or charges twice. |
POST /v1/batch (CreateAsync) and POST /v1/batch/{id}/retry (RetryAsync) |
Never. A retry could be charged twice. |
Wait before a retry: the Retry-After value if the server sent one (at most 60 s), otherwise 0.5 s × 2^n plus 0 to 250 ms of jitter. A TIMEOUT is not retried. While GetAsync or WaitAsync poll, a 429 waits for Retry-After and polling goes on; it does not count as a failure.
Development
dotnet test tests/YouTubeTranscriptNet.Tests
# End-to-end against a real API (never prints the key):
export YTT_API_KEY=ytt_...
export YTT_BASE_URL=http://127.0.0.1:3461 # optional
dotnet run --project examples/E2E
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.