Skip to content

JSON API

You'll open part of your app to other programs through a documented JSON API with keys, paging and honest errors, then watch someone else's code use it.

About

So far your app has served browsers. An API serves programs: a script, a phone shortcut, a spreadsheet, a friend's site. That means your URLs become promises, your errors have to be readable by something that can't guess, and you can't rename a field without telling someone. You'll expose your guestbook, blog or app through a handful of JSON endpoints, document them well enough for a stranger, and then get a client you didn't write to use them. Your language's built-in JSON support and your existing app are all you need, so skip the gateway products and the spec tooling for now.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Three weeks
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Web Building to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Serve a list and a single item as JSON at stable URLs. Something like /api/v1/entries and /api/v1/entries/42, with a Content-Type header that says application/json. Put the version in the path from day one, because the second version always comes. Take a screenshot of curl -i fetching both, with the headers in the shot. Photo
  2. 2
    Page the list and say how to get the next page. Return twenty items at a time with a next link or cursor in the body, and a total if it's cheap to count. A list with no paging works until the table has ten thousand rows and then takes the server down. Take a screenshot of the second page's response with the next link visible. Photo
  3. 3
    Return errors a program can read. Use the right status code (400, 401, 404, 429) and a JSON body with a short code and a plain-words message, in the same shape every time. Ask for an item that doesn't exist and for a page that isn't a number. Take a screenshot of both responses. Photo
  4. 4
    Issue API keys and require one for anything that writes. Let a logged-in user create a key in their settings, show it once, store only a hash of it, and read it from the Authorization header. Reads can stay public if the data is; writes need a key and a rate limit. Film a POST failing without a key and succeeding with one. Video
  5. 5
    Write the docs on one page with a working example for every endpoint. Each endpoint gets the method, the path, the parameters, a curl command that works and the exact response. Put it at /api on your site. Someone who has never seen your app should be able to make a call in two minutes. Take a screenshot of the docs page. Photo
  6. 6
    Get a client you didn't write to use it. A friend's script, a Shortcuts automation on a phone, a spreadsheet import, or a second program of your own written on another day in another language. Set the CORS headers if the client runs in a browser. Take a screenshot of the other client showing your data, or film it running, with the matching request in your server log. Photo or video