📖 Scenario: You are building a simple REST API for a bookstore. You want to create clear documentation for one of your API endpoints so that other developers can understand how to use it.
🎯 Goal: Create a structured documentation for a REST API endpoint that shows the HTTP method, URL path, description, request parameters, and example response.
📋 What You'll Learn
Create a dictionary called endpoint_doc with keys: method, path, description, parameters, and response_example
Set method to the HTTP method as a string
Set path to the URL path as a string
Set description to a short explanation string
Set parameters to a list of dictionaries, each with name, type, and required keys
Set response_example to a dictionary showing a sample JSON response
Print the endpoint_doc dictionary at the end
💡 Why This Matters
🌍 Real World
Clear API documentation helps developers understand how to use your web services correctly and quickly.
💼 Career
API documentation skills are essential for backend developers, API designers, and technical writers working with web services.
Progress0 / 4 steps
1
Create the basic endpoint documentation dictionary
Create a dictionary called endpoint_doc with keys method, path, and description. Set method to "GET", path to "/books", and description to "Retrieve a list of books".
Rest API
Hint
Use curly braces {} to create a dictionary and assign the keys and values exactly as instructed.
2
Add request parameters list
Add a key parameters to the endpoint_doc dictionary. Set it to a list with two dictionaries: first with name as "author", type as "string", and required as False; second with name as "year", type as "integer", and required as False.
Rest API
Hint
Remember that parameters is a list of dictionaries. Use square brackets [] for the list and curly braces {} for each dictionary.
3
Add example response dictionary
Add a key response_example to the endpoint_doc dictionary. Set it to a dictionary with a key books whose value is a list of two dictionaries. The first dictionary has id: 1, title: "The Great Gatsby", author: "F. Scott Fitzgerald". The second dictionary has id: 2, title: "1984", author: "George Orwell".
Rest API
Hint
Use nested dictionaries and lists to represent the example JSON response structure.
4
Print the endpoint documentation
Write a print statement to display the endpoint_doc dictionary.
Rest API
Hint
Use print(endpoint_doc) to display the dictionary.
Practice
(1/5)
1. What is the main purpose of endpoint documentation in a REST API?
easy
A. To explain how to use each API URL including methods and parameters
B. To store user data securely
C. To improve the speed of the API server
D. To design the user interface of a website
Solution
Step 1: Understand the role of endpoint documentation
Endpoint documentation describes how to use each API URL, including methods and parameters.
Step 2: Compare options with this role
Only To explain how to use each API URL including methods and parameters matches this purpose; others describe unrelated tasks.
Final Answer:
To explain how to use each API URL including methods and parameters -> Option A
Quick Check:
Endpoint documentation = Usage guide [OK]
Hint: Think: What helps users understand API URLs? [OK]
Common Mistakes:
Confusing documentation with data storage
Mixing API documentation with UI design
Assuming documentation improves server speed
2. Which of the following is the correct way to document the HTTP method in an endpoint documentation?
easy
A. "method": "GET"
B. "method" = GET
C. method: GET;
D. GET
Solution
Step 1: Identify proper JSON syntax for documentation
Endpoint docs often use JSON or similar formats; key-value pairs use colon and quotes for strings.
Step 2: Check each option's syntax
"method": "GET" uses correct JSON syntax with quotes and colon; others use invalid or non-standard syntax.
Final Answer:
"method": "GET" -> Option A
Quick Check:
JSON key-value = colon + quotes [OK]
Hint: Remember JSON syntax: key in quotes, colon, value in quotes [OK]
But the API returns a 400 error when you send the request. What is the most likely mistake in the documentation?
medium
A. The endpoint URL is missing a trailing slash
B. The HTTP method should be GET, not POST
C. Parameters should be inside a "requestBody" section, not "parameters"
D. The response code 201 is incorrect for creation
Solution
Step 1: Understand POST request structure
POST requests usually send data in the request body, not as URL parameters.
Step 2: Check documentation structure
Parameters listed under "parameters" implies URL/query parameters; for POST data, it should be under "requestBody".
Final Answer:
Parameters should be inside a "requestBody" section, not "parameters" -> Option C
Quick Check:
POST data = requestBody, not parameters [OK]
Hint: POST data goes in requestBody, not parameters [OK]
Common Mistakes:
Confusing query parameters with request body
Changing HTTP method incorrectly
Ignoring correct response codes
5. You want to document an endpoint that updates a user's email. It uses PATCH method at /users/{id}. The request body must include email as a string. The response returns 200 with updated user data or 400 if email is invalid. Which is the best way to structure this documentation snippet?