API Directory › 🎉 Fun & Random › xkcd
No authHTTPS

xkcd API

Fetch xkcd comics and their metadata via a simple JSON endpoint.

Auth
No auth needed
HTTPS
Yes
CORS
Unknown
Open xkcd docs ↗

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

https://xkcd.com

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
curl -s https://xkcd.com/info.0.json                # latest
curl -s https://xkcd.com/327/info.0.json            # "Exploits of a Mom"
Python (random comic)
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

numComic number
title / safe_titleTitle (safe_title strips characters that break filenames)
imgDirect image URL
altThe hover/title text — often the actual punchline
year / month / dayPublication date
transcriptText transcript (populated for older comics only)

Common errors

404Unknown 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.

🛠️ Handy LarzOS tools

Free tools to help you work with this API.

JSON Formatter

Related Fun & Random APIs

← Back

More Fun & Random APIs · All 196 APIs