What is Swagger? A Beginner’s Guide with example

Last updated on : Jun 25, 2023 by Ashwini Upadhyay

In this tutorial, we will learn what is Swagger and How can we generate API documentation using Swagger?
Swagger is an open-source software framework that helps developers design, build, document, and consume RESTful APIs (Application Programming Interfaces). It provides a standardized way to describe and visualize APIs, making it easier for developers to understand and interact with them.

Swagger uses the OpenAPI Specification (OAS), which is a specification for describing RESTful APIs. The specification is written in YAML or JSON and defines the endpoints, request/response formats, parameters, authentication methods, and other details of the API.

How does Swagger help with API documentation?

Swagger provides a way to generate interactive and human-readable API documentation based on the OpenAPI Specification. By annotating your API with Swagger/OpenAPI annotations, you can automatically generate documentation that includes information about endpoints, request/response formats, parameters, and more. This makes it easier for developers to understand and consume your API.

Can Swagger be used for API testing?

Yes, Swagger can be used for API testing. The Swagger ecosystem includes tools that allow you to generate API clients and server stubs from the OpenAPI Specification. These generated clients can be used to programmatically interact with your API, making it easier to write and execute tests.

Swagger Editor Tool

How to create an API document using Swagger and include a runnable example, you can use the Swagger Editor tool. Follow the steps below to create an API document with a simple example:

  • Go to the Swagger Editor website (editor.swagger.io).
  • Clear the default code in the editor.
  • Replace the code with the following example:

Here’s an example of an API document using Swagger with both YAML and JSON examples:

openapi: 3.0.0
info:
  title: User API
  description: An example API for managing user data
  version: 1.0.0
paths:
  /users:
    get:
      summary: Get all users
      description: Retrieves a list of all users.
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
              example:
                - id: 1
                  name: John Doe
                - id: 2
                  name: Jane Smith
            application/yaml:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
              example:
                - id: 1
                  name: John Doe
                - id: 2
                  name: Jane Smith
    post:
      summary: Add a new user
      description: Adds a new user to the system.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewUser'
              example:
                name: John Doe
          application/yaml:
            schema:
              $ref: '#/components/schemas/NewUser'
              example:
                name: John Doe
      responses:
        '201':
          description: User created successfully

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
    NewUser:
      type: object
      properties:
        name:
          type: string
  • The Swagger Editor will display the response with the result of the User API.

As you can see in this screenshot.

swagger editor

In this example, the API document defines a User API. It starts with the OpenAPI version, title, description, and version information.

The /users endpoint has two operations: GET and POST. The GET operation retrieves a list of all users, and the POST operation adds a new user to the system.

For the GET the operation, the API documentation includes response examples in both JSON and YAML formats. The response schema is defined using the $ref keyword, referring to the User schema. The examples show an array of user objects with id and name properties.

The POST operation includes a request body with examples provided in both JSON and YAML formats. The request body schema refers to the NewUser schema, which only includes the name property.

The components the section defines the reusable schemas. In this example, there are two schemas: User and NewUser. The User schema represents the structure of an user object, while the NewUser schema represents the structure of an object used for creating new users.

By including YAML examples in the API document, it provides a comprehensive reference for developers and API consumers who prefer working with YAML-based data formats. The examples demonstrate the structure of request and response data, allowing users to understand how to interact with the API effectively.

ashwini upadhyay

Ashwini Upadhyay

I'm a full-stack developer, owner of LogicalDuniya.I love to write tutorials and tips that can help to other artisan. I love to write on PHP, Laravel, Angular, Vue, React, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Press ESC to close