[ DOCS ] /docs/card/api
HTTP API
The card studio is a front end for three endpoints, served by Cloudflare Pages Functions from https://junaadh.com. This page describes what they do, from the code. For the language the scripts are written in, see the card guide.
Endpoints#
| Request | Returns |
|---|---|
GET /api/card/<login>.svg | The card, as an SVG image. |
GET /api/card/<login>.json | The profile data a script reads, as JSON. The studio draws locally from it. |
POST /api/card/scripts | Saves a script and returns its id, for short links. |
GET /api/card/<login>.svg#
<login> must be a GitHub username: letters, digits and hyphens, starting with a letter or digit, at most 39 characters. Anything else is a 400. The .svg extension is optional; without any extension you get the SVG.
Query parameters#
The script comes from exactly one of these, checked in this order:
| Name | Meaning |
|---|---|
script | A saved script id (11 letters and digits, from POST /api/card/scripts). 400 if it is not an id or nothing is saved under it. |
s | A script carried in the URL, as the studio encodes it: deflate-compressed and base64url (leading z), or plain base64url (leading p). At most 6,144 characters, and 24 KB once decoded. 400 if it cannot be decoded. |
preset | A built-in script. Default classic. An unknown id is a 400 that lists the valid ones: classic, languages, heatmap, badge, terminal, synthwave, repos, minimal. |
Every other parameter is handed to the script, which reads it with card.param(name, fallback). script, s and preset are never passed on. The limits:
- at most 16 such parameters;
- names of 1 to 32 characters from
A-Z a-z 0-9 _ . -; - values of up to 100 characters;
- if a name is repeated, the first value is used.
A request that breaks one of these is a 400 with a message saying which.
https://junaadh.com/api/card/octocat.svg
https://junaadh.com/api/card/octocat.svg?preset=terminal&accent=%23f0883e
https://junaadh.com/api/card/octocat.svg?preset=classic&theme=light
https://junaadh.com/api/card/octocat.svg?script=Kx9fQ2mZ7aB&theme=lightA script that does not compile, or faults, still gets status 200: the body is the report drawn as an image (see errors as images), so it shows in a README. It is not cached. Problems with the data or the request are not 200; see status codes.
GET /api/card/<login>.json#
The profile the script sees, plus avatar, which is a data: URL (or an empty string) and is only used by card.avatar. Query parameters are ignored.
login, name, bio, location strings (name falls back to the login)
followers, following, repos, stars, forks numbers
commits (this calendar year), pullRequests, issues, contributedTo numbers (0 without a token)
joined year the account was created
contributions last 365 days
days daily contributions, oldest first, up to 371
languages up to 8 of { name, color, share }
top up to 6 of { name, description, language, stars, forks }
avatar data: URL or ""Responses carry cache-control: public, max-age=3600 and an x-card-source header (graphql or rest) that tells you how complete the data is. Errors are JSON: { "error": "…" }.
POST /api/card/scripts#
Saves a script so an embed can say ?script=<id> instead of carrying it. The body is the script text, sent as text/plain.
curl -X POST https://junaadh.com/api/card/scripts \
-H 'content-type: text/plain' \
--data-binary @card.eit{ "id": "Kx9fQ2mZ7aB" }- The id is 11 characters derived from the SHA-256 of the script. The same script always has the same id, and an id that exists is never overwritten.
- The script is rendered once against a sample profile and must finish cleanly. A script that does not compile, or that faults on that render, is refused with 422; one that passes is stored even if it later faults for particular data.
- The body is read with a hard cap of 16 KB whatever the
content-lengthsays. The request must come from this site: itsOriginheader has to match, or the answer is 403. - Scripts are kept for good, because short links live in READMEs. Rate limiting is done at the edge, not by this endpoint.
- Scripts are stored in a Cloudflare KV namespace. Without one the endpoint answers 501.
| Status | When | Body |
|---|---|---|
| 200 | Saved, or already saved. | { "id" } |
| 400 | The body is empty or not valid UTF-8. | { "error" } |
| 403 | The Origin header is missing or is not this site. | { "error" } |
| 413 | More than 16 KB (16,384 bytes). | { "error" } |
| 415 | The content-type does not start with text/plain. | { "error" } |
| 422 | The script does not compile, or faults when rendered against the sample profile. | { "error", "report" }, the report being the compiler's diagnostic |
| 501 | No KV namespace is bound (short links are not configured). | { "error" } |
Every response from this endpoint is cache-control: no-store. The functions send no CORS headers, so the studio calls it from the same origin.
Caching#
- GitHub data: 1 hour. Profile data is cached per login (case-insensitive) and refetched once it is an hour old. A user GitHub does not know is remembered for 10 minutes, and a rate limit or upstream failure for 1 minute, so repeated bad requests do not reach GitHub each time.
- SVG: 1 hour, then stale while it refreshes. The response says
public, max-age=3600, stale-while-revalidate=86400. On the server a good card is kept for a day; after the first hour it is still served at once while a new one is drawn in the background. The cache key is the login and the query with its parameters sorted, so the same card is one entry whatever order the parameters came in. A cached card is found from the query alone, before any saved script is looked up or anysis decoded. - Errors are never cached. Every error response, including the image for a script with a mistake, says
no-storeand is not kept on the server.
Headers#
| Header | Value |
|---|---|
content-type | image/svg+xml; charset=utf-8 (application/json for .json and the scripts endpoint) |
content-security-policy | default-src 'none'; style-src 'unsafe-inline'; img-src data: on every SVG, errors included |
x-content-type-options | nosniff on every SVG and JSON response |
cache-control | As in caching: public, max-age=3600, stale-while-revalidate=86400 for a card, public, max-age=3600 for .json, no-store for errors |
x-card-source | graphql when the data came from GitHub's GraphQL API with a token, rest for the fallback |
x-card-note | Only with rest: no GITHUB_TOKEN: public data only; contributed-to is empty |
Status codes#
| Status | Meaning |
|---|---|
| 200 | A card, or the diagnostic image for a script that does not compile or faults. |
| 400 | Invalid username; too many, badly named or too long parameters; unknown preset; script that is not an id or is not saved; s that is too long or cannot be decoded. |
| 404 | GitHub has no such user. |
| 414 | The query string is longer than 2,048 characters, not counting the s value (which has its own limit of 6,144 characters). |
| 500 | The card could not be rendered (an unexpected error). |
| 502 | GitHub failed or refused: an error status, a rejected token, an unanswerable query or the anonymous rate limit. |
Error bodies match the request: an SVG error card (headed eitri // card api) for .svg requests so it is visible in a README, and { "error": "…" } for .json.
Without a GitHub token#
With a GITHUB_TOKEN secret on the server the data comes from GitHub's GraphQL API: only public data is requested, private repositories are never counted, and contributions come from the public calendar. Without one, the API falls back to GitHub's public, unauthenticated endpoints (the REST and search APIs and the public contributions page) and the data has small gaps. The response says which with x-card-source.
contributedTois 0: GitHub has no public source for it.daysandcontributionsare read from the public contributions page,commits(this calendar year),pullRequestsandissuesfrom the search API. Each of these falls back to 0 (or an emptydays) if GitHub refuses; anonymous search allows about 10 requests a minute per IP, and results are cached for an hour.languagescome from each repository's main language (each repository counts once), with built-in colours for common languages (grey for the rest), instead of per-language byte counts.- Only the 100 most recently pushed repositories the user owns are looked at, minus forks. GitHub's anonymous rate limit is low; when it is hit the card answers 502 until the limit resets.