📖 Scenario: You are building a simple API for a bookstore. You want to describe your API clearly so other developers can understand how to use it. OpenAPI Specification (Swagger) helps you write this description in a standard way.
🎯 Goal: Create a basic OpenAPI Specification file that describes a bookstore API with one endpoint to get a list of books.
📋 What You'll Learn
Create the main OpenAPI info section with title and version
Define the server URL where the API is hosted
Add a path for /books with a GET method
Describe the response for the GET method with status 200 and a JSON content type
💡 Why This Matters
🌍 Real World
OpenAPI Specification is used to describe REST APIs clearly so developers can understand and use them easily.
💼 Career
Knowing how to write and read OpenAPI specs is important for backend developers, API designers, and technical writers.
Progress0 / 4 steps
1
Set up the basic OpenAPI info
Create a variable called openapi_spec and assign it a dictionary with the keys "openapi" set to "3.0.0", and "info" which is another dictionary containing "title" set to "Bookstore API" and "version" set to "1.0.0".
Rest API
Hint
Use nested dictionaries to create the info section inside openapi_spec.
2
Add the server URL
Add a key "servers" to the openapi_spec dictionary. Set it to a list with one dictionary inside that has the key "url" set to "https://api.bookstore.com".
Rest API
Hint
The servers key holds a list of server objects. Each server object has a url.
3
Add the /books GET endpoint
Add a key "paths" to openapi_spec. It should be a dictionary with a key "/books". This key maps to another dictionary with a key "get". The "get" dictionary should have a key "responses" which is a dictionary with a key "200". The "200" key maps to a dictionary with a key "description" set to "A list of books" and a key "content" which is a dictionary with a key "application/json" that maps to an empty dictionary.
Rest API
Hint
Use nested dictionaries to describe the path, method, and response structure.
4
Print the OpenAPI Specification
Write a print statement to display the openapi_spec dictionary.
Rest API
Hint
Use print(openapi_spec) to show the whole specification.
Practice
(1/5)
1. What is the main purpose of the OpenAPI Specification in REST APIs?
easy
A. To store user data securely
B. To execute API requests automatically
C. To replace the need for API servers
D. To describe the API endpoints, methods, and data formats in a standard way
Solution
Step 1: Understand OpenAPI's role
OpenAPI Specification is designed to describe APIs clearly and consistently.
Step 2: Identify the correct purpose
It documents endpoints, HTTP methods, parameters, and data formats for APIs.
Final Answer:
To describe the API endpoints, methods, and data formats in a standard way -> Option D
Quick Check:
OpenAPI = API description standard [OK]
Hint: OpenAPI is for describing APIs, not running or storing data [OK]
Common Mistakes:
Confusing OpenAPI with API execution tools
Thinking OpenAPI stores user data
Believing OpenAPI replaces servers
2. Which of the following is the correct way to define an HTTP GET method for the path /users in OpenAPI YAML?
easy
A. paths:
get:
/users:
summary: Get all users
B. paths:
/users:
get:
summary: Get all users
C. get:
paths:
/users:
summary: Get all users
D. paths:
/users:
post:
summary: Get all users
Solution
Step 1: Check OpenAPI path structure
Paths are keys under paths, then HTTP methods like get are nested under each path.
Step 2: Validate correct nesting and method
paths:
/users:
get:
summary: Get all users correctly places /users under paths and get under /users.
Final Answer:
paths:
/users:
get:
summary: Get all users -> Option B
C. Response code 200 should be a string '200', not a number
D. Path should start with a capital letter
Solution
Step 1: Check response code format
OpenAPI requires response codes as strings, e.g., '200', not numbers.
Step 2: Verify other fields
Summary is allowed; requestBody is optional; path case is flexible.
Final Answer:
Response code 200 should be a string '200', not a number -> Option C
Quick Check:
Response codes must be strings [OK]
Hint: Always quote response codes as strings in YAML [OK]
Common Mistakes:
Using numeric response codes
Assuming requestBody is mandatory
Thinking summary is invalid
5. You want to document an API that returns a list of products with optional filtering by category. Which OpenAPI feature helps you define the optional category filter parameter for the GET /products endpoint?
hard
A. Use a query parameter with required: false under the GET method
B. Add a path parameter named category in the URL
C. Include category in the requestBody schema
D. Define category as a header parameter
Solution
Step 1: Understand parameter types
Optional filters are usually query parameters, not path or header parameters.
Step 2: Define optional query parameter
Set required: false for the category query parameter under GET.
Final Answer:
Use a query parameter with required: false under the GET method -> Option A
Quick Check:
Optional filters = query params with required false [OK]
Hint: Filters usually go as optional query parameters [OK]
Common Mistakes:
Using path parameters for optional filters
Putting filters in requestBody for GET
Defining filters as headers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.