xkcd API
Fetch xkcd comics and their metadata via a simple JSON endpoint.
What it does
The xkcd JSON API returns metadata for any xkcd comic: the image URL, title, the hover-text (alt), publication date, comic number and a transcript for older strips. There is an endpoint for the latest comic and one for a specific number.
Base URL
Authentication
None. /info.0.json for the current comic, /{num}/info.0.json for comic number num. No CORS headers — call it from a backend or via a proxy, not directly from browser JavaScript.
Code examples
curl -s https://xkcd.com/info.0.json # latest curl -s https://xkcd.com/327/info.0.json # "Exploits of a Mom"
import requests, random
latest = requests.get("https://xkcd.com/info.0.json", timeout=10).json()["num"]
n = random.randint(1, latest)
c = requests.get(f"https://xkcd.com/{n}/info.0.json", timeout=10).json()
print(c["num"], c["title"], c["img"])Rate limits & quotas
No key, no documented rate limit — it is static JSON per comic, so cache aggressively (a given comic never changes). To pick a random comic, fetch the latest number once and randomise locally. Comic 404 deliberately does not exist (it 404s, as a joke).
Key response fields
| num | Comic number |
| title / safe_title | Title (safe_title strips characters that break filenames) |
| img | Direct image URL |
| alt | The hover/title text — often the actual punchline |
| year / month / day | Publication date |
| transcript | Text transcript (populated for older comics only) |
Common errors
| 404 | Unknown comic number — and comic 404 is an intentional 404. |
| (CORS error in browser) | Expected — proxy the request server-side. |
| (missing transcript) | Newer comics have an empty transcript; don’t rely on it. |
When to use it (and alternatives)
For ‘comic of the day’ features, random-xkcd buttons, and embedding a relevant strip in docs or Slack. Remember the alt text — showing the image without it loses half the joke.
FAQ
How do I get a random comic?
Fetch /info.0.json for the latest num, generate a random integer from 1 to that, then fetch /{n}/info.0.json. Skip 404 if you land on it.
Why does fetch() fail in my web app?
xkcd’s API sends no Access-Control-Allow-Origin header. Proxy it through your own server (which also lets you cache).
Is the hover text available?
Yes — it is the alt field. Display it as a caption or tooltip; for many comics it is the real payoff.
New to APIs?
Short guides for working with this API — and any other.