Scority

YouTube API JavaScript Guide

Use server-side JavaScript to fetch YouTube transcripts with the Scority API. Keep API keys out of browser code and handle transcript errors explicitly.

Direct answer

Use JavaScript on the server, not in the browser

A YouTube transcript request needs an API key, so the request should run in trusted server-side JavaScript. Browser code can call your own backend route, but it should not call Scority directly with x-api-key.

  • Use fetch from a server runtime, route handler, worker or backend job.
  • Store SCORITY_API_KEY in server-side environment variables.
  • Send video_id or video_url, not both.
  • Use language when you need a specific caption language.

Server-side fetch by video ID

const apiKey = process.env.SCORITY_API_KEY

if (!apiKey) {
  throw new Error("SCORITY_API_KEY is required")
}

const url = new URL("https://api.scority.ai/v1/youtube/transcript")
url.searchParams.set("video_id", "dQw4w9WgXcQ")
url.searchParams.set("language", "en")

const response = await fetch(url, {
  headers: {
    "x-api-key": apiKey,
    accept: "application/json",
  },
})

const body = await response.json()

if (!response.ok) {
  throw new Error(body.error?.code ?? "transcript_request_failed")
}

Backend route wrapper

export async function POST(request: Request) {
  const { videoId } = await request.json()
  const url = new URL("https://api.scority.ai/v1/youtube/transcript")
  url.searchParams.set("video_id", videoId)

  const response = await fetch(url, {
    headers: {
      "x-api-key": process.env.SCORITY_API_KEY ?? "",
      accept: "application/json",
    },
  })

  return Response.json(await response.json(), {
    status: response.status,
  })
}
Request shape

Build the URL with query parameters

Use URL and URLSearchParams instead of string concatenation. It avoids encoding mistakes when you switch from video_id to video_url.

  • video_id is best when your app already stores the 11-character YouTube ID.
  • video_url is useful when users paste full YouTube links.
  • language and lang are aliases; use one.
  • The response includes text, segments, source and language when captions are available.
Errors

Handle API errors as product states

A transcript integration should not treat every non-200 as a crash. Some responses describe input, quota or caption availability.

  • 401 unauthorized means x-api-key is missing, invalid or revoked.
  • 429 rate_limited means wait for Retry-After or reduce burst traffic.
  • 429 quota_exceeded means monthly quota is exhausted.
  • 404 transcript_not_available means captions are not available for that request.
  • 502 upstream_transcript_failed may be retryable later.
Browser safety

Do not expose x-api-key in frontend JavaScript

If your UI needs transcripts, call your own backend first. That backend can validate the user, apply product rules and call Scority with the API key.

Docs

Authentication

Keep API keys server-side and rotate exposed keys.

Open →
Docs

Error codes

Use normalized error codes for application behavior.

Open →
Guide

Node.js guide

Read the Node-specific version with backend fetch examples.

Open →
Reference

API reference

Review parameters, response fields and rate-limit headers.

Open →
OpenAPI

OpenAPI specification

Import the machine-readable contract into API tooling.

Open →
Limits

Quota and rate limits

Plan retries, caching and quota behavior.

Open →