Skip to content

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Word API

English | 日本語

A tiny, dependency-free REST API that serves random words from a 670,741-word English list and a 290,586-word list of Japanese words written only in kanji. Runs on Cloudflare Pages Functions — no database, no KV, no bundler.

Live demo · MIT License

curl https://word-api.pages.dev/word
# {"word":"isothermally"}

Features

  • 670,741 English words served straight from a static text file
  • 290,586 Japanese words written only in kanji, such as 快晴, 鬱憤 and 憤怒, merged from 27 sources: JMdict, Wiktionary in 18 languages, Japanese WordNet, Wikidata, SudachiDict, Mozc, UniDic, NAIST-jdic, IPAdic and UniDic-CSJ. The Japanese endpoints work exactly like the English ones, and kanji words never appear in the English endpoints
  • GET and POST — pass parameters in the query string or a JSON body
  • Zero storage cost — no KV/D1; the word lists are static assets, cached in memory per isolate
  • CORS enabled — callable from any browser origin
  • Consistent JSON errors with proper status codes (400, 404, 405, 413, 500)

Endpoints

/random/, /all/ and /search/ each have an english and a japanese endpoint, which take the same parameters and return the same shapes.

Method Path Description
GET / POST /word One random English word, or N distinct words with ?count=N (1–100)
GET / POST /random/english Same as /word
GET / POST /random/japanese One random Japanese word, or N distinct words with ?count=N (1–100)
GET / POST /all/english?limit=100&offset=0 Paginated list of all English words (limit ≤ 1000, default 100). /all is an alias
GET / POST /all/japanese?limit=100&offset=0 Paginated list of all Japanese words
GET / POST /search/english?q=app Search English words (mode=prefix|contains|suffix, limit ≤ 1000). /search is an alias
GET / POST /search/japanese?q=快 Search Japanese words
GET / POST /words/:word Check whether a word exists. Words written only in kanji are checked against the Japanese list (see Japanese words)
GET /words.txt Raw English list, one word per line
GET /kanji.txt Raw Japanese list, one word per line
GET / Landing page with a live demo

Every response is JSON (except /words.txt, /kanji.txt and /). HEAD works wherever GET does, unsupported methods return 405, and unknown paths return 404.

Examples

# One random word
curl https://word-api.pages.dev/word
# {"word":"freighting"}

# Five random words
curl "https://word-api.pages.dev/word?count=5"
# {"words":["realist","trehalose","bew","cantare","quantometer"],"count":5}

# Three random Japanese words
curl "https://word-api.pages.dev/random/japanese?count=3"
# {"words":["水辺地区","空盒","配電器"],"count":3}

# Paginate the whole list
curl "https://word-api.pages.dev/all?limit=3&offset=0"
# {"words":["0day","0days","0x"],"count":3,"total":670741,"limit":3,"offset":0}

curl "https://word-api.pages.dev/all/japanese?limit=3&offset=100"
# {"words":["一万回","一万坪","一万夜"],"count":3,"total":290586,"limit":3,"offset":100}

# Prefix / substring / suffix search
curl "https://word-api.pages.dev/search?q=apple&limit=5"
# {"words":["apple","applebaum","applebee","applebees","appleberry"],"count":5,"query":"apple","mode":"prefix","limit":5}

curl "https://word-api.pages.dev/search?q=ing&mode=suffix&limit=3"
# {"words":["aahing","abalienating","abanding"],"count":3,"query":"ing","mode":"suffix","limit":3}

curl "https://word-api.pages.dev/search/japanese?q=日本&mode=contains&limit=3"
# {"words":["上代日本語","中世日本語","中古日本語"],"count":3,"query":"日本","mode":"contains","limit":3}

# Existence check
curl https://word-api.pages.dev/words/apple
# {"word":"apple","exists":true,"length":5}

curl https://word-api.pages.dev/words/快晴
# {"word":"快晴","exists":true,"length":2}

curl -w " [%{http_code}]\n" https://word-api.pages.dev/words/zzzznotaword
# {"word":"zzzznotaword","exists":false} [404]

POST

Send the same parameters as a JSON body. Body values take precedence over the query string; an empty body is fine. The body may be up to 16 KB.

curl -X POST https://word-api.pages.dev/word \
  -H 'content-type: application/json' -d '{"count":3}'
# {"words":["notaeums","chelodine","codd"],"count":3}

curl -X POST https://word-api.pages.dev/search \
  -H 'content-type: application/json' -d '{"q":"ing","mode":"suffix","limit":2}'

curl -X POST https://word-api.pages.dev/all/japanese \
  -H 'content-type: application/json' -d '{"limit":2,"offset":10}'

For /words/:word, a word in the body takes precedence over the path, and the query string is not used.

Japanese words

The Japanese list holds words written only in kanji (Han characters, including 々). Words that mix kanji and kana, such as 食べる or お茶, are not part of it.

/words/:word checks the Japanese list when :word consists only of kanji and the English list otherwise, so 食べる returns 404. Percent-encoded paths (/words/%E9%AC%B1%E6%86%A4) and POST bodies ({"word":"憤怒"}) work as well. Input is normalized to Unicode NFC, so CJK compatibility ideographs such as U+FA68 match their standard form (難, U+96E3). length counts characters, so 𠮟 has a length of 1.

curl https://word-api.pages.dev/words/鬱憤
# {"word":"鬱憤","exists":true,"length":2}

curl -X POST https://word-api.pages.dev/words/x \
  -H 'content-type: application/json' -d '{"word":"憤怒"}'
# {"word":"憤怒","exists":true,"length":2}

curl -w " [%{http_code}]\n" https://word-api.pages.dev/words/汉字
# {"word":"汉字","exists":false} [404]

Errors

Status Body Cause
400 {"error":"count must be an integer between 1 and 100"} Out-of-range or non-numeric parameter (numbers in the query string must be plain digits, so 1e2 is rejected)
400 {"error":"query parameter 'q' is required"} /search without q
400 {"error":"mode must be a string"} A JSON body value of the wrong type
400 {"error":"invalid JSON body"} Malformed POST body
404 {"word":"...","exists":false} Word not in the list
404 {"error":"not found"} Unknown path
405 {"error":"method not allowed"} Anything other than GET / HEAD / POST / OPTIONS
413 {"error":"request body must be at most 16384 bytes"} POST body larger than 16 KB
500 {"error":"internal error"} Unexpected server error

How it works

  • data/words.txt (~6.6 MB) and data/kanji.txt (~3 MB), both sorted with one word per line, are copied to public/ at build time and served as static assets.
  • The Functions load each list once per isolate via env.ASSETS.fetch() and keep the text in module scope together with the start offset of every line (Uint32Array), about 13 MB for both lists. The load is passed to ctx.waitUntil(), so it completes even if the request that started it is cancelled while other requests are waiting for it.
  • Existence checks and prefix searches use binary search; substring and suffix searches scan the text with indexOf.
  • Random selection uses crypto.getRandomValues() with rejection sampling (no modulo bias), and the words in one response are distinct.
  • public/_routes.json serves the static files without invoking Functions, and a catch-all Function returns a JSON 404 for every other unknown path.
  • The data is read-only, so no KV, D1, or R2 is involved.

Project structure

.
├── data/
│   ├── words.txt              # English word list (670,741 lines)
│   └── kanji.txt              # Japanese kanji word list (290,586 lines), generated by scripts/build-kanji.mjs
├── functions/
│   ├── _lib/words.ts          # Shared: list loading/cache, search, JSON responses, CORS, request parsing, RNG
│   ├── _lib/kanji.ts          # The Japanese list and kanji detection
│   ├── [[path]].ts            # JSON 404 for unknown paths
│   ├── word.ts                # GET/POST /word
│   ├── random/english.ts      # GET/POST /random/english (same as /word)
│   ├── random/japanese.ts     # GET/POST /random/japanese
│   ├── all.ts                 # GET/POST /all
│   ├── all/english.ts         # GET/POST /all/english (same as /all)
│   ├── all/japanese.ts        # GET/POST /all/japanese
│   ├── search.ts              # GET/POST /search
│   ├── search/english.ts      # GET/POST /search/english (same as /search)
│   ├── search/japanese.ts     # GET/POST /search/japanese
│   └── words/[word].ts        # GET/POST /words/:word
├── public/
│   ├── index.html             # Landing page
│   ├── _routes.json           # Paths that invoke Functions
│   ├── words.txt              # Generated by `npm run build` (git-ignored)
│   └── kanji.txt              # Generated by `npm run build` (git-ignored)
├── scripts/
│   └── build-kanji.mjs        # Downloads the dictionaries and builds data/kanji.txt
├── NOTICE                     # Third-party data notices
├── wrangler.toml
├── package.json
└── tsconfig.json

Development

Requires Node.js 22.12+ (the minimum for Wrangler 4.168.0). Nothing needs to be installed: the scripts run Wrangler through npx (pinned to 4.168.0), so no node_modules is created in the project.

npm run build         # data/words.txt, data/kanji.txt -> public/
npm run dev           # http://localhost:8788
npm run update:kanji  # re-download the dictionaries and regenerate data/kanji.txt

npm run build checks that data/words.txt is sorted without duplicates, because lookups use binary search, and copies the data files into public/. Pages Functions are served directly by Wrangler, so there is no bundling step.

npm run update:kanji downloads about 1.3 GB of dictionaries. Set KANJI_SOURCES=/path/to/dir to keep the downloads and reuse them on the next run. Downloads older than 7 days are fetched again, except the versioned UniDic, Open JTalk and SudachiDict archives.

When you add a static file to public/, also add its path to exclude in public/_routes.json; otherwise the catch-all Function answers it with 404.

Word list

data/words.txt contains one lowercase word per line, sorted in ASCII order. It is a de-duplicated merge of:

  • SCOWL / GNU Aspell dictionary entries
  • WordNet 3.1 lemma data
  • OPTED headwords from the 1913 Webster's Unabridged Dictionary
  • Moby Word Lists by Grady Ward
  • Public-domain Project Gutenberg classics, including the King James Bible, Shakespeare, Beowulf, the Canterbury Tales, the Iliad, the Odyssey, the Aeneid, the Divine Comedy, Le Morte d'Arthur, and Paradise Lost

Classic-text words are kept only when they occur at least twice across the corpus, which filters out most OCR noise while preserving archaic vocabulary and proper nouns.

public/words.txt is generated from data/words.txt by npm run build and is not tracked in Git. To change the list, edit data/words.txt, keep it sorted (LC_ALL=C sort -u), rebuild, and restart the dev server.

See NOTICE for third-party copyright notices.

Kanji word list

data/kanji.txt contains one word per line, sorted in UTF-16 code unit order (JavaScript's default sort order). npm run update:kanji (scripts/build-kanji.mjs, no dependencies) downloads the sources below, normalizes every headword to Unicode NFC and keeps those written only in kanji: CJK unified ideographs plus 々, 〇 and 〻, without radicals. The numbers are how many words each source lists; most words appear in several sources.

Dictionaries:

  • JMdict by the Electronic Dictionary Research and Development Group (EDRDG) — 132,496 words. Search-only kanji forms are skipped.
  • Wiktionary: the Japanese entries of 18 language editions, extracted by Kaikki.org — Japanese (74,448), English (44,966), Chinese (20,826), Korean (6,255), Russian (5,244), French (5,209), Thai (3,454), Czech (2,866), Polish (2,557), Kurdish (2,135), Portuguese (1,815), Vietnamese (1,772), Italian (1,579), Malay (939), Dutch (732), Turkish (473), Greek (412) and German (161)
  • Japanese WordNet — 41,452 words
  • Wikidata lexemes — 15,733 words

Morphological analysis and input method dictionaries:

  • SudachiDict by Works Applications (small, core and notcore) — 139,590 words
  • Mozc, Google's open-source Japanese input method — 120,668 words
  • UniDic 2.1.2 by the UniDic Consortium — 78,722 words
  • NAIST-jdic, as bundled with Open JTalk 1.11 — 47,984 words
  • IPAdic by NAIST — 45,496 words
  • UniDic-CSJ, as bundled with Open JTalk 1.11 — 43,844 words

Proper nouns are skipped wherever a source marks them: Wiktionary name entries, Wikidata proper-noun lexemes and the proper nouns of the morphological dictionaries, which alone hold about a million person and place names. From the morphological dictionaries only nouns, pronouns, adjectival nouns, adverbs, adnominals, conjunctions and interjections are taken, without affixes. Wiktionary entries for single characters and symbols are skipped too.

Because JMdict and Wiktionary are licensed under CC BY-SA 4.0, data/kanji.txt is distributed under CC BY-SA 4.0. The EDRDG licence requires online services to keep the data current, so rerun npm run update:kanji and redeploy regularly (at least monthly).

Contributing

Issues and pull requests are welcome. If you change the word list, run npm run build before opening a pull request.

License

Code is MIT. data/kanji.txt is CC BY-SA 4.0 (see Kanji word list). Third-party notices are in NOTICE.

Releases

Packages

Contributors

Languages