CHAPTER 26
Beginner
API Documentation with Swagger/OpenAPI
Updated: May 13, 2026
5 min read
# CHAPTER 26
API Documentation with Swagger/OpenAPI
1. Introduction
If you build a flawless API but don't tell anyone how to use it, it is entirely useless. Frontend developers need to know exact URLs, required headers, JSON payload formats, and status codes. Writing this manually in a Word document is tedious and quickly becomes outdated. In Chapter 26, we will introduce the OpenAPI Specification (formerly Swagger), the absolute industry standard for documenting REST APIs.2. Learning Objectives
By the end of this chapter, you will be able to:- Explain what the OpenAPI Specification is.
- Understand the relationship between OpenAPI and Swagger.
-
Read and write a basic
openapi.yamlfile.
- Describe how interactive documentation works.
- Understand the value of auto-generated documentation.
3. Beginner-Friendly Explanation
Imagine buying a complex piece of IKEA furniture. You need a manual that shows exactly what screws go where. An API Documentation is the manual for your API. OpenAPI is a standardized language for writing that manual. Instead of writing it in English, you write it in a structured format (YAML or JSON). Swagger UI takes that structured YAML file and magically turns it into a beautiful, interactive webpage. Developers can actually press buttons on the webpage to test your API directly from the documentation!4. Real-World Examples
- Stripe / Twilio: These companies are famous for their world-class API documentation. They use OpenAPI specs behind the scenes to generate SDKs and interactive web pages.
- Swagger UI: Look up "Swagger Petstore". It is a live example of an interactive documentation page generated purely from an OpenAPI file.
5. Detailed Code Examples
Here is an example of what anopenapi.yaml file looks like for a simple /users endpoint.
yaml
6. Request/Response Examples
When developers visit your Swagger UI page, they see theGET /users endpoint. They can click a "Try it out" button, type 2 into the page parameter box, and hit execute. The Swagger UI will construct the URL (https://api.mywebsite.com/v1/users?page=2) and make the HTTP request for them!
7. HTTP Examples
The OpenAPI file strictly defines what HTTP methods are allowed for each path. Under/users, we define get:. We could also define post: under the exact same path to document how to create a user.
8. JSON Examples
OpenAPI strongly encourages defining reusable "Schemas". If you define aUser schema once, you can reference it in GET responses, POST payloads, and PUT payloads without rewriting it.
yaml
9. Best Practices
-
Design-First API Development: Write your OpenAPI
.yamlfile *before* you write any PHP code. This allows you to plan the exact JSON structures and share them with the frontend team so they can start working immediately using mock servers.
-
Use Annotations in PHP: Writing massive YAML files by hand is hard. Advanced PHP developers use tools like
zircote/swagger-php. You write specially formatted comments directly above your PHP controller functions, and the tool automatically generates theopenapi.jsonfile for you!
10. Common Mistakes
- Outdated Documentation: Writing a manual Word document and forgetting to update it when you change the API is the #1 complaint of frontend developers. Using OpenAPI (especially via PHP code annotations) ensures your documentation is always perfectly synced with your code.
11. Mini Exercises
- 1. Search Google for "Swagger Petstore". Open the link.
-
2.
Click on the
GET /pet/{petId}row.
- 3. Click "Try it out". Enter an ID (like 1 or 2). Click "Execute" and view the real API response right in the browser.
12. Coding Challenges
Challenge 1: Look at the YAML example in Section 5. Add a404 response definition under the responses: block, describing what happens if the URL is not found.
13. MCQs with Answers
Question 1
What is the industry standard specification for defining REST APIs?
Question 2
What does Swagger UI do?
Question 3
Why is "Design-First" API development beneficial?
14. Interview Questions
- Q: Explain the relationship between OpenAPI and Swagger.
- Q: Why is an interactive documentation tool like Swagger UI superior to a standard PDF manual?
-
Q: Describe how using code annotations (like
swagger-php) solves the problem of outdated documentation.