OpenAPI
Category: Development
What is OpenAPI?
OpenAPI Specification (formerly known as Swagger) is a standard, language-independent format for describing RESTful APIs. It allows humans and computers to understand the capabilities of an API without access to the source code, documentation, or through network traffic.
OpenAPI provides a unified way for documenting, designing, building, and consuming RESTful web services.
Main characteristics of OpenAPI:
- Machine-readable format - YAML or JSON
- Language-independent - works with any programming language
- Automation - code generation, documentation and clients
- Standardized - supported by Linux Foundation
- Extensible - can be extended with custom properties
History and evolution:
2010
Swagger created by Tony Tam in SmartBear Software
2015
OpenAPI Initiative created under Linux Foundation
2016
OpenAPI 3.0 - significantly improved version
2021
OpenAPI 3.1 - compatibility with JSON Schema 2020-12
Main structure of OpenAPI document:
- OpenAPI Object - Root object, which contains the main information about the API
- Info Object - Metadata about the API - title, version, description
- Servers Object - URLs of the servers hosting the API
- Paths Object - Paths (endpoints) and operations of the API
- Components Object - Repeating components - schemas, parameters, responses
- Security Object - Definition of security schemes and requirements
Tools and ecosystem:
- Documentation - Swagger UI, ReDoc, Stoplight
- Development - Swagger Editor, OpenAPI Generator, Swagger Codegen
- Testing - Schemathesis, Dredd, Postman
- Integration - SpringDoc OpenAPI, Swashbuckle, FastAPI, Express OpenAPI
Advantages of using OpenAPI:
For developers
- Single source of truth for the API
- Automatic documentation generation
- Code generation for clients and servers
- Easier onboarding for new developers
For business
- Reduces development time
- Improves the quality of the API
- Improves communication between teams
- Standardizes processes for API development
For users
- Clear and accurate documentation
- Interactive API exploration
- Easier integration with the API
- Automatically generated clients
Typical workflow with OpenAPI:
- Design First - Designing the API through the OpenAPI specification before implementation
- Code Generation - Generating server stubs and client SDKs
- Implementation - Implementing business logic based on the generated code
- Documentation - Automatically generating up-to-date documentation
- Testing - Testing the API against the specification
- Maintenance - Maintaining and updating the specification with changes
Good practices for OpenAPI:
- Use Design-First approach - design before coding
- Organize the specification with $ref for reuse
- Describe all responses - success and errors
- Define security schemes clearly
- Include examples for all schemas and parameters
- Validate the specification with tools
- Version properly your API
- Keep the specification synchronized with the implementation
Real applications of OpenAPI:
- Банкови системи - OpenAPI се utilizet banks APIs for payment systems and transactions
- E-commerce платформи - Големи e-commerce компании използват OpenAPI за своите публични APIs
- Cloud providers - AWS, Google Cloud and Azure use OpenAPI-like formats for their APIs
- SaaS companies - Many SaaS companies provide OpenAPI specifications for their integration APIs
- Governmental organizations - OpenAPI is used to standardize governmental APIs for data and services