📖 Scenario: Imagine you are creating a simple REST API for a book store. You want other developers to use your API easily. Good documentation helps them understand how to use your API quickly and correctly.
🎯 Goal: Build a small REST API with clear documentation comments that explain each endpoint. This will show how documentation helps others adopt your API.
📋 What You'll Learn
Create a basic REST API with at least two endpoints
Add clear documentation comments for each endpoint
Use descriptive names and simple explanations
Show how documentation helps users understand the API
💡 Why This Matters
🌍 Real World
Good API documentation helps developers quickly understand how to use your services, reducing confusion and support requests.
💼 Career
Many software jobs require writing clear API documentation to help teams and external users adopt your code efficiently.
Progress0 / 4 steps
1
Set up the basic REST API structure
Create a simple REST API using Flask. Define an app variable with Flask(__name__) and create two endpoints: /books and /authors that return simple JSON lists.
Rest API
Hint
Remember to import Flask and jsonify, then create the app and define two routes with functions returning JSON lists.
2
Add configuration for documentation style
Add a variable called doc_style and set it to 'simple' to represent the style of documentation you will add.
Rest API
Hint
Just create a variable named doc_style and assign it the string 'simple'.
3
Add documentation comments to each endpoint
Add a docstring to the get_books function that says: """Return a list of book titles.""" and to the get_authors function that says: """Return a list of author names.""".
Rest API
Hint
Use triple quotes right after the function definition to add the docstring.
4
Run the app and print confirmation
Add the code to run the Flask app with app.run() inside a if __name__ == '__main__': block. Then print "API is running with simple documentation style".
Rest API
Hint
Use the if __name__ == '__main__' block to run the app and print the message before starting.
Practice
(1/5)
1. Why is good documentation important for a REST API?
easy
A. It helps users understand how to use the API correctly.
B. It makes the API run faster.
C. It hides the API's internal code.
D. It reduces the number of API endpoints.
Solution
Step 1: Understand the role of documentation
Documentation explains how to use the API, making it easier for users to understand its functions.
Step 2: Identify the correct benefit
Helping users understand usage is the main purpose, not performance or hiding code.
Final Answer:
It helps users understand how to use the API correctly. -> Option A
Quick Check:
Documentation improves understanding = D [OK]
Hint: Documentation explains usage clearly to users [OK]
Common Mistakes:
Confusing documentation with performance optimization
Thinking documentation hides code
Believing documentation reduces API endpoints
2. Which of the following is the correct way to include an example in REST API documentation?
easy
A. Write the API code without any comments.
B. Only list the API endpoint URL without details.
C. Show a sample request and response with clear explanations.
D. Use complex technical terms without examples.
Solution
Step 1: Identify what makes examples helpful
Examples should show how to call the API and what response to expect, with clear explanations.
Step 2: Compare options
Options B, C, and D lack clarity or helpful examples, making them poor documentation choices.
Final Answer:
Show a sample request and response with clear explanations. -> Option C
Quick Check:
Examples with requests and responses = A [OK]
Hint: Examples must show request and response clearly [OK]
Common Mistakes:
Skipping examples in documentation
Using only URLs without context
Writing code without explanation
3. Given this API documentation snippet:
{
"endpoint": "/users",
"method": "GET",
"description": "Returns a list of users",
"example_response": [{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]
}
What will a user expect when calling this API?
medium
A. A list of user objects with id and name fields.
B. An error message because the method is wrong.
C. A single user object with detailed info.
D. No response because the endpoint is missing.
Solution
Step 1: Analyze the documentation snippet
The endpoint "/users" with method GET returns a list of users as shown in example_response.
Step 2: Understand the expected output
The example_response shows an array of user objects with id and name, so the user expects this list.
Final Answer:
A list of user objects with id and name fields. -> Option A
Quick Check:
GET /users returns list of users = A [OK]
Hint: Match example response to expected output [OK]
Common Mistakes:
Confusing list with single object
Assuming error without reason
Ignoring example response
4. You find this documentation snippet:
Endpoint: /items Method: POST Description: Adds a new item Example request: {"name": "Book"}
But users report errors when calling the API. What is likely missing?
medium
A. The API endpoint URL.
B. The API server IP address.
C. The HTTP method used.
D. The example response showing success or error messages.
Solution
Step 1: Review the provided documentation
The snippet shows endpoint, method, description, and example request but no example response.
Step 2: Identify what users need to avoid errors
Without example response, users don't know what to expect or how to handle errors properly.
Final Answer:
The example response showing success or error messages. -> Option D
Quick Check:
Missing example response causes confusion = B [OK]
Hint: Always include example responses in docs [OK]
Common Mistakes:
Ignoring example responses
Assuming endpoint or method is missing
Thinking server IP is needed in docs
5. You want to improve adoption of your REST API. Which combination of documentation features will most likely help?
hard
A. Using technical jargon and skipping examples to save time.
B. Clear endpoint descriptions, example requests and responses, and error explanations.
C. Only listing endpoints without details to keep docs short.
D. Providing code snippets without explaining parameters.
Solution
Step 1: Identify key documentation features that aid adoption
Clear descriptions, examples, and error info help users understand and trust the API.
Step 2: Evaluate other options
Options A, B, and D omit important details or clarity, which reduces adoption.
Final Answer:
Clear endpoint descriptions, example requests and responses, and error explanations. -> Option B
Quick Check:
Complete, clear docs increase adoption = C [OK]
Hint: Combine clarity, examples, and error info in docs [OK]
Common Mistakes:
Thinking shorter docs are better
Using jargon without explanation
Skipping error handling info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.