Skip to content

Classics Book Picker API Reference

About this document

Context/Internal Note: The API was built using FastAPI with SQLite. I haven't shared the code on GitHub or hosted the API anywhere. This exercise is meant for an evaluation of my documentation style, not the code. The base URL in this doc is a placeholder, because the API isn't hosted. This is the base URL I am using throughout this doc: https://classicsbookpicker.example.com.

Audience will be the developers who are familiar with JSON and want to build apps or websites using this API.

I used AI to code and tested the API myself. I wrote the doc myself but had AI review it for clarity.

For testing I first ran the application in VS Code:

Running the app locally

Then, I made the calls to the API as shown here:

Get moods

Get recommendations by mood and length

Get recommendations by mood

Get a random recommendation

Get a random recommendation by length

Introduction

This API finds a classic book by mood and length. In this API, a book means a short story, novella, novel, or long novel. The database has 32 books. All endpoints use GET and return JSON. It's a read-only API. No authentication is needed.

Getting Started

You can use the Classics Book Picker API in an app or a website to include a "What should I read" feature or just a browsable reading list.

The following table lists the endpoints.

Endpoint What it does
GET /books List books, with filters and pages
GET /books/{id} Get one book
GET /recommendations Get recommended books for a mood
GET /recommendations/random Get a random book
GET /moods List the valid moods

Make your first call, starting with the moods endpoint.

curl "https://classicsbookpicker.example.com/moods"

The API responds with the list of moods:

{
  "moods": [
    "adventurous",
    "dark",
    "funny",
    "light",
    "romantic",
    "spooky",
    "thoughtful"
  ]
}

You can use the moods for calls to other endpoints. For example, ask for a recommendation for funny short stories:

curl "https://classicsbookpicker.example.com/recommendations?mood=funny&length=short-story"

The call responds with 3 books with a short note on each book:

{
  "count": 3,
  "results": [
    {
      "id": 1,
      "title": "The Open Window",
      "author": "Saki",
      "length": "short-story",
      "moods": [
        "funny",
        "spooky"
      ],
      "note": "A nervous visitor hears a ghostly tale from a quick-witted girl, and the ending shows how much she enjoys making things up."
    },
    {
      "id": 7,
      "title": "The Overcoat",
      "author": "Nikolai Gogol",
      "length": "short-story",
      "moods": [
        "funny",
        "thoughtful"
      ],
      "note": "A humble clerk saves for a new coat, and what follows is both absurdly funny and quietly sad."
    },
    {
      "id": 31,
      "title": "Jeeves Takes Charge",
      "author": "P. G. Wodehouse",
      "length": "short-story",
      "moods": [
        "funny",
        "light"
      ],
      "note": "Bertie Wooster hires a new valet called Jeeves, who quickly proves much cleverer than his employer."
    }
  ]
}

count is the number of books returned, and results is the list. Each book has an id, a title, an author, a length, one or more moods, and a one-sentence note.

A book can have several moods. The Open Window, for example, is both funny and spooky.

Length describes the form of the book:

Value Meaning
short-story A single story, read in one short sitting. For example, The Open Window.
novella A short book, roughly under 45,000 words. For example, Strange Case of Dr Jekyll and Mr Hyde.
novel A full-length book, roughly 45,000 to 300,000 words. For example, Pride and Prejudice.
long-novel A very long novel, roughly over 300,000 words. For example, Middlemarch.

You used the mood and length parameters to narrow the results in the previous API call.

Note: Every parameter accepts only one value. For example, mood cannot accept both funny and thoughtful. Additionally, the parameter value is case-sensitive. Funny is not the same as funny.

In the next call to the API, use pagination. GET /books returns results in pages. Two parameters control them:

Parameter Default Allowed values Meaning
page 1 1 or more Which page to return
limit 10 1 to 50 Maximum number of books per page

If you call https://classicsbookpicker.example.com/books, the API returns the first 10 books because the default page is 1 and the default limit is 10 books per page.

The API has a total of 32 books. With the default limit of 10, that makes four pages: three of ten books and a last page of two. To read the second page:

curl "https://classicsbookpicker.example.com/books?page=2" 

Suppose you want to return the first and second pages, with 3 books on each page.

curl "https://classicsbookpicker.example.com/books?page=1&limit=3" 

The API will respond with the first page, listing 3 books.

Then, call the second page using the same limit.

curl "https://classicsbookpicker.example.com/books?page=2&limit=3" 

The API will respond with the second page, listing the next 3 books.

The next section describes all five endpoints in detail.

Endpoints

Here's when to use each endpoint:

I want to... Use
Browse the whole collection, page by page GET /books
Look up one book when I know its id GET /books/{id}
Get a short list of books for a mood GET /recommendations
Get one surprise book GET /recommendations/random
Show the valid moods, for example in a menu GET /moods

Get Books

Lists books, with optional filters and pagination. Results are ordered by id.

Request Parameters

Name Type Required Default Description
mood string No None Only books with this mood. Valid values are adventurous, dark, funny, light, romantic, spooky, and thoughtful.
length string No None Only books of this length. Valid values are short-story, novella, novel, and long-novel.
page integer No 1 Page number. Must be 1 or more.
limit integer No 10 Maximum number of books per page. Must be 1 to 50.

Example Request

curl "https://classicsbookpicker.example.com/books?length=novel&limit=4"

Response Fields

Field Type Description
total integer Number of books that match your filters, across all pages
page integer The page returned
limit integer The page size used
results array The books on this page
message string Present only when results is empty

Example Response

{
  "total": 13,
  "page": 1,
  "limit": 4,
  "results": [
    {
      "id": 16,
      "title": "Pride and Prejudice",
      "author": "Jane Austen",
      "length": "novel",
      "moods": [
        "romantic",
        "funny",
        "light"
      ],
      "note": "Elizabeth Bennet and Mr Darcy must get past pride and first impressions before they can admit what they feel."
    },
    {
      "id": 17,
      "title": "Emma",
      "author": "Jane Austen",
      "length": "novel",
      "moods": [
        "funny",
        "romantic"
      ],
      "note": "A confident young matchmaker keeps misreading everyone's hearts, including her own."
    },
    {
      "id": 18,
      "title": "Three Men in a Boat",
      "author": "Jerome K. Jerome",
      "length": "novel",
      "moods": [
        "funny",
        "light"
      ],
      "note": "Three friends and a dog set off for a relaxing trip up the Thames and cause chaos at every stop."
    },
    {
      "id": 19,
      "title": "Dracula",
      "author": "Bram Stoker",
      "length": "novel",
      "moods": [
        "spooky",
        "dark",
        "adventurous"
      ],
      "note": "Told through letters and diary entries, it follows a vampire count who moves to England and the group who hunt him."
    }
  ]
}

Errors

Status error When
400 invalid_mood mood is not a valid mood
400 invalid_length length is not a valid length
400 invalid_parameter page or limit is not an integer, page is less than 1, or limit is outside 1 to 50

Example Request

curl "https://classicsbookpicker.example.com/books?page=0"

Error Response

{
  "error": "invalid_parameter",
  "message": "'page' must be an integer of 1 or more."
}

Filters that match no books, and a page past the last page, are not errors. They return 200 with an empty results list and a message.

Example Request

curl "https://classicsbookpicker.example.com/books?length=novel&page=3"

Empty Response

{
  "total": 13,
  "page": 3,
  "limit": 10,
  "results": [],
  "message": "Page 3 is past the last page. With limit=10 there are 2 pages."
}

There are 13 novels in the database. With a limit of 10 books per page, third page doesn't contain anything.

Get a Book by ID

Returns one book.

Request Parameters

Name Type Required Description
id integer Yes The book's id, given in the path.

Example Request

curl "https://classicsbookpicker.example.com/books/23"

Response Fields

A single book object.

Example Response

{
  "id": 23,
  "title": "Treasure Island",
  "author": "Robert Louis Stevenson",
  "length": "novel",
  "moods": [
    "adventurous"
  ],
  "note": "A boy finds a pirate's map and sails for buried treasure with a crew he can't quite trust."
}

Errors

Status error When
400 invalid_parameter id is not an integer, for example /books/abc
404 not_found No book has that id. This includes 0 and negative numbers.

Example Request

curl "https://classicsbookpicker.example.com/books/33"

Error Response

{
  "error": "not_found",
  "message": "No book found with id 33."
}

The ID 33 doesn't exist, because the API has 32 books.

To see the IDs of all 32 books, use Get Books.

curl "https://classicsbookpicker.example.com/books?page=1&limit=32"

Get Recommendations

Suggests books for a mood, optionally narrowed by length. Results are ordered by id, so the same request always returns the same books.

Request Parameters

Name Type Required Default Description
mood string Yes None The mood you want. Valid values are adventurous, dark, funny, light, romantic, spooky, and thoughtful.
length string No None Narrow the suggestions to one length. Valid values are short-story, novella, novel, and long-novel.
limit integer No 5 Maximum number of books to return. Must be 1 to 20.

Example Request

curl "https://classicsbookpicker.example.com/recommendations?mood=thoughtful&length=novella"

Response Fields

Field Type Description
count integer Number of books in results
results array The matching books, up to limit
message string Present only when results is empty

count is the number of books in this response, not the number of matches. If count equals your limit, there may be more matches. Raise limit, up to 20, to see them.

Example Response

{
  "count": 4,
  "results": [
    {
      "id": 6,
      "title": "The Call of the Wild",
      "author": "Jack London",
      "length": "novella",
      "moods": [
        "adventurous",
        "thoughtful"
      ],
      "note": "A dog's wilderness adventure celebrating resilience and freedom."
    },
    {
      "id": 12,
      "title": "The Death of Ivan Ilyich",
      "author": "Leo Tolstoy",
      "length": "novella",
      "moods": [
        "thoughtful",
        "dark"
      ],
      "note": "An ordinary, comfortable official faces his final illness and starts to question how he has lived."
    },
    {
      "id": 13,
      "title": "A Christmas Carol",
      "author": "Charles Dickens",
      "length": "novella",
      "moods": [
        "spooky",
        "light",
        "thoughtful"
      ],
      "note": "A miserly man is visited by ghosts on Christmas Eve and given one chance to change."
    },
    {
      "id": 14,
      "title": "The Time Machine",
      "author": "H. G. Wells",
      "length": "novella",
      "moods": [
        "adventurous",
        "thoughtful"
      ],
      "note": "An inventor travels far into the future and finds a world very different from the one he hoped for."
    }
  ]
}

Errors

Status error When
400 missing_parameter mood is missing
400 invalid_mood mood is not a valid mood
400 invalid_length length is not a valid length
400 invalid_parameter limit is not an integer, or is not between 1 and 20

Example Request

curl "https://classicsbookpicker.example.com/recommendations?length=novella"

Error Response

{
  "error": "missing_parameter",
  "message": "'mood' is required. See GET /moods for allowed values."
}

Filters that match no books are not an error. They return 200 with "count": 0, an empty results list and a message.

Example Request

curl "https://classicsbookpicker.example.com/recommendations?mood=funny&length=novella"

Empty Response

{
  "count": 0,
  "results": [],
  "message": "No books match mood 'funny' with length 'novella'. Try removing the length filter."
}

Get a Random Recommendation

Returns one book, chosen at random from the books that match. Calling it again can return a different book.

Request Parameters

Name Type Required Default Description
mood string No None Only pick from books with this mood.
length string No None Only pick from books of this length.

With no parameters, the pick is from all the books.

Example Request

curl "https://classicsbookpicker.example.com/recommendations/random?mood=spooky&length=novella"

Response Fields

A single book object. The book you get may differ from the following example.

Example Response

{
  "id": 15,
  "title": "The Turn of the Screw",
  "author": "Henry James",
  "length": "novella",
  "moods": [
    "spooky",
    "dark"
  ],
  "note": "A governess becomes convinced her two young charges are haunted, and the story never confirms whether she is right."
}

Errors

Status error When
400 invalid_mood mood is not a valid mood
400 invalid_length length is not a valid length
404 no_match No book matches the filters

Unlike GET /recommendations, this endpoint returns an error when nothing matches, because it must return exactly one book.

Example Request

curl "https://classicsbookpicker.example.com/recommendations/random?mood=dark&length=long-novel"

Error Response

{
  "error": "no_match",
  "message": "No books match mood 'dark' with length 'long-novel'. Try removing the length filter."
}

Get Moods

Returns every valid mood, in alphabetical order. Use these values for the mood parameter.

Request Parameters

None.

Example Request

curl "https://classicsbookpicker.example.com/moods"

Response Fields

Field Type Description
moods array of strings Every valid mood

Example Response

{
  "moods": [
    "adventurous",
    "dark",
    "funny",
    "light",
    "romantic",
    "spooky",
    "thoughtful"
  ]
}

Errors

None. This endpoint takes no parameters.