Basic API Blueprint Tutorial with Polls API
- https://apiary.io
- http://bit.ly/api-design-tutorial (this repo)
- https://apiblueprint.org/documentation/tutorial.html
- http://dredd.org/en/latest/
$ npm install -g dredd
$ dredd
$ docker run -it -v $PWD:/api -w /api apiaryio/dredd dredd
$ dredd <My API Design file> <My running service URL>
Files in repo are keyed to Tutorial steps. E.g. apiary1.apib
is the solution to Step 1 below.
- Go into Apiary and “Create New API Project”
- Keep “API Blueprint” Selected and name the API
- You now have a working API using a template - Save
- Optional: Sync the API to GitHub:
- Create a new repo in GitHub
- In Apiary “Link this Project to GitHub”
- On your Laptop:
$ git clone
your repo
- Find the
/questions
resource - Copy the response and paste above existing one
- Change the
201
to a200
- Save - Optional:
- Push to GitHub from Apiary
$ git pull
on your Laptop- test with
$ dredd apiary.apib http://polls.apiblueprint.org/
- Add an optional
{?page}
numeric parameter with default value 1 to the/questions
resource - Save - Optional:
- Push to GitHub from Apiary
$ git pull
on your Laptop- test with
$ dredd apiary.apib http://polls.apiblueprint.org/
- Add
/questions/{question_id}
as the first resource in your API. The parameter should be a number with default value 1 and is required - A
GET
method retrieves the question’s detail. Give an example JSON response (hint: it should be the same as an element in the/questions
response array) - Save - Optional:
- Push to GitHub from Apiary
$ git pull
on your Laptop- test with
$ dredd apiary.apib http://polls.apiblueprint.org/
- Add a resource to use as a voting mechanism on the choices of a question
- Nesting parameters will work here:
/questions/{question_id}/choices/{choice_id}
POST
is a nice way to tell our service we are voting Response should be a201
, no response body needed - Save- Optional: push from Apiary, pull on your laptop and test
- We want an easy way to discover what the API can do
- Easiest way is to add a top-level root “
/
“ resource that tells us what other resources are available in the API - Response looks like:
{"questions_url": "/questions"}
- Save - Optional: push from Apiary, pull on your laptop and test
- We want a quick way to list the choices to vote on
- Add a resource
/questions/{question_id}/choices
GET
should return an array of choices (hint: look at thePOST /questions
request body) - Save- Optional: test with dredd
- What we specced out has not been implemented!
$ echo $?
gives exit code: 0=success, >1=failure- Hard to detect by eyeballing
- Hard to write tests by hand and keep them in sync
- Important to have so you don’t break clients
- API Design + Dredd in your CI/CD
- https://help.apiary.io/ more about Apiary
- http://bit.ly/learn-api full API Management tutorial
- https://apiary.io/how-to-build-api interactive guide