Introduction
TypeScript types and zod/mini validators for the Top.gg API.
topgg-api-types is a TypeScript types and validators package for the Top.gg API. It has no runtime logic beyond validation, so it’s a good fit whether you’re calling the API
yourself, verifying incoming webhooks, or just want accurate types.
Installation
What’s included
- Types —
topgg-api-types/v1andtopgg-api-types/v0for the current and legacy API. - Validators —
topgg-api-types/v1/validatorsandtopgg-api-types/v0/validators, zod/mini schemas matching every exported type one-to-one. - REST clients —
topgg-api-types/v1/client(TopGGClient) andtopgg-api-types/v0/client(TopGGLegacyClient), thin wrappers over nativefetch. - OAuth client —
topgg-api-types/v1/oauth(TopGGOAuthClient) for the OAuth 2.1 authorization code + PKCE flow. - Webhook verification —
topgg-api-types/v1/webhook(verifyWebhookSignature) for thex-topgg-signatureheader on vote/integration webhooks. - Route builders —
topgg-api-types/v1/routesandtopgg-api-types/v0/routes, standalone if you want to make requests yourself.
Types only
Most projects just need the types, for static checking and IntelliSense — no runtime overhead:
Runtime validation
Import from the validators subpath when you need to check a response or webhook payload at
runtime:
The validators are written with zod/mini, not full zod — a lighter-weight functional-API variant with a smaller bundle size.
REST client
Skip writing your own fetch wrapper:
Pass validateResponses: true to validate responses against this package’s own zod/mini schemas
before they’re returned:
Errors: non-2xx responses throw TopGGAPIError (status, type, title, detail, and retryAfter when the API sends a Retry-After header). getVoteStatus is the one exception — a
404 there resolves to null instead of throwing, since that’s the documented “user hasn’t voted”
response.
getProjects() lists every project covered by the current credential, paginated the same way
as getProjectVotes below. It needs an OAuth access token or application token — unlike every other TopGGClient method, it does not work with a plain project token:
Pagination: vote and project listings are paginated by cursor. The returned page has a next() method for fetching the following page; cursor is always present, even on the last
page, so stop paging once data comes back empty rather than checking cursor:
For the deprecated legacy API, use TopGGLegacyClient from topgg-api-types/v0/client — same
shape, but the Authorization header is sent raw (no Bearer prefix), matching the v0 API.
OAuth client
For apps that act on behalf of Top.gg users (OAuth 2.1 authorization code + PKCE), use TopGGOAuthClient from topgg-api-types/v1/oauth. It builds the authorization URL,
exchanges/refreshes tokens, and revokes authorizations. It holds your client secret, so use it
only from a backend:
See the authorization guide for the full flow,
including PKCE code_verifier/code_challenge generation.
Routes
Both client subpaths also export a Routes object — functions building the relative REST path for
each endpoint, in the same style as discord-api-types’s Routes. Useful if you want to make
requests yourself without pulling in the full client:
Webhook signature verification
topgg-api-types/v1/webhook exports verifyWebhookSignature, for the x-topgg-signature header
Top.gg sends with vote.create/integration.* webhook requests (HMAC-SHA256 over {timestamp}.{rawBody}, keyed with your integration’s webhook_secret). It needs the raw request body — verify before you parse it as JSON:
It’s built on the Web Crypto API (crypto.subtle), so it runs unmodified in Node 19+, browsers,
Deno, Bun, and Workers — no node:crypto dependency.
For the deprecated v0 API, webhook auth is a raw string comparison instead: Top.gg sends your
configured secret back in the Authorization header, so check req.headers.authorization === yourSecret yourself — no crypto involved.
Choosing an approach
- Types only — smallest bundle, best IntelliSense, no runtime overhead. Right for most users.
- + Validators — adds runtime validation, for when API drift or malformed webhook payloads need to be caught at runtime, not just compile time.
- + Client — skip writing your own fetch wrapper; auth, error handling, and pagination come built in.
They compose — use just types, or types + validators + client together.
Next
Browse the generated API Reference for every exported type, schema, and client method.