Menu

Integrations / Cookbook and starters

Next.js search app

A search page with a popularity cut and a source picker, rendered on the server so the key never reaches the browser.

Copy as MarkdownOpen in ChatGPT (opens in a new tab)Open in Claude (opens in a new tab)

AvailableTypeScript, Next.js 16Three filesKey stays on the serverTested 2026-11-19

The page is a server component: the form submits with a plain GET, the server calls POST /v1/search with the key from its environment, and the browser receives finished HTML. There is no client JavaScript of ours, and no route a visitor could call to use your key for other searches.

1Get the code

Four files: package.json, lib/millionshort.ts, app/page.tsx and app/layout.tsx.

Choose one
JSON
{
  "name": "millionshort-nextjs-search",
  "private": true,
  "description": "A search page on MillionShort: Next.js, the key kept on the server.",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "^16.4.6",
    "react": "^19.3.1",
    "react-dom": "^19.3.1",
    "server-only": "^0.0.1"
  },
  "devDependencies": {
    "@types/node": "^26.6.6",
    "@types/react": "^19.3.0",
    "typescript": "^7.0.2"
  }
}

2Run it

Shell
npm install
export MILLIONSHORT_API_KEY=ms-YOUR_API_KEY
npm run dev

Open the address next dev prints, and search. To deploy, set MILLIONSHORT_API_KEY as a server-side environment variable of your host; nothing else is needed.

3What you get

A form, then ten results with each site's popularity band and date. The cut defaults to "Without the top 1,000"; the source picker switches between web, news, discussions and academic. The page says what the search cost, and when a search fails it says why (the API's error code), rather than showing an error page.

4Change it

ToChange
Offer more sourcesAdd them to SOURCES (each searched source costs 1 credit)
Show passages, not snippetsAdd max_highlights: 2 to the body and render highlights
Let a model answer tooCall a model with the results on the server, as in RAG with fresh pages

Costs and limits

1 credit per search. Anyone who can open the page can search with your key: put the page behind your own sign-in, or add a rate limit, before you make it public.

When it fails

What you seeWhyFix
401 AUTH_MISSING_KEY or AUTH_INVALID_KEYThe key is missing or wrongSet MILLIONSHORT_API_KEY; keys start with ms-
400 INVALID_PARAMA field name or value the API does not takeThe message names the field; the search reference lists them all
429 RATE_LIMITED or CAPACITYToo many calls a minute, or no free capacity right nowWait the Retry-After seconds, then retry
429 INSUFFICIENT_CREDITSThe plan's credits are used upStop and add credits; do not retry
"The search did not work: AUTH_MISSING_KEY" after deployingThe key is not set on the hostSet MILLIONSHORT_API_KEY in the host's server environment, not as a public variable
The build fails on server-onlyA client component imports lib/millionshort.tsKeep the search in server code: that error is what keeps the key out of the browser

This page as Markdown · llms.txt