<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vyankatesh S Repal</title>
    <description>The latest articles on Forem by Vyankatesh S Repal (@vyankatesh_srepal_a8193d).</description>
    <link>https://forem.com/vyankatesh_srepal_a8193d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1938221%2Fe0b25a59-d999-436c-bbe2-9dd0f29897d3.jpg</url>
      <title>Forem: Vyankatesh S Repal</title>
      <link>https://forem.com/vyankatesh_srepal_a8193d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vyankatesh_srepal_a8193d"/>
    <language>en</language>
    <item>
      <title>A Beginner's Guide to Writing OpenAPI Specifications</title>
      <dc:creator>Vyankatesh S Repal</dc:creator>
      <pubDate>Fri, 16 Aug 2024 13:54:13 +0000</pubDate>
      <link>https://forem.com/vyankatesh_srepal_a8193d/a-beginners-guide-to-writing-openapi-specifications-ig7</link>
      <guid>https://forem.com/vyankatesh_srepal_a8193d/a-beginners-guide-to-writing-openapi-specifications-ig7</guid>
      <description>&lt;p&gt;If you're new to API development, you've likely heard of OpenAPI. It's a powerful tool that helps you define, understand, and document your APIs in a standardized way. In this guide, we'll walk through the basics of creating an OpenAPI Specification (OAS) using a sample specification for Payment APIs. By the end, you'll know how to set up your own OAS and even generate code based on openAPI specification using Swagger tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with OpenAPI
&lt;/h2&gt;

&lt;p&gt;OpenAPI Specification (OAS) is a standard for describing RESTful APIs. It allows developers to describe the structure of their APIs so that machines can read them, making it easier to generate documentation and code for the APIs through specification. OAS are usually prepared in either YAML or JSON format. This blog illustrates an YAML example. We can convert any YANL specification using JSON and vice versa using tools like &lt;a href="https://www.bairesdev.com/tools/json2yaml/" rel="noopener noreferrer"&gt;YAML to JSON converter&lt;/a&gt;.&lt;br&gt;
To check OpenAPI map, refer to link: &lt;a href="https://openapi-map.apihandyman.io/" rel="noopener noreferrer"&gt;https://openapi-map.apihandyman.io/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mandatory Properties in OpenAPI
&lt;/h2&gt;

&lt;p&gt;Before diving into the details, it's important to understand the mandatory properties required in an OpenAPI specification:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;openapi: The version of the OpenAPI Specification you're using (e.g., 3.0.3).&lt;/li&gt;
&lt;li&gt;info: Metadata about the API, such as the title, description, version, and contact information.&lt;/li&gt;
&lt;li&gt;paths: The available API paths and operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Security Definitions
&lt;/h2&gt;

&lt;p&gt;Security is a critical aspect of APIs. You can define security schemes globally or at the individual API level. Here’s how you can do it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Global Security:&lt;/strong&gt; You can define security globally using the security property at the root level. This will apply to all API operations unless overridden.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;security:&lt;br&gt;
  -BasicAuth: []&lt;br&gt;
  -BearerAuth: []&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Security Schemes in Components:&lt;/strong&gt; Use the securitySchemes under the components object to define reusable security schemes. Components are described in further part of this blog.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;components:&lt;br&gt;
  securitySchemes:&lt;br&gt;
    BasicAuth:&lt;br&gt;
      type: http&lt;br&gt;
      scheme: basic&lt;br&gt;
    BearerAuth:&lt;br&gt;
      type: http&lt;br&gt;
      scheme: bearer&lt;br&gt;
      bearerFormat: JWT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. API-specific Security:&lt;/strong&gt; You can also apply security to specific APIs using the security property within each path operation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;paths:&lt;br&gt;
  /payment/status:&lt;br&gt;
    get:&lt;br&gt;
      security:&lt;br&gt;
        - BearerAuth: []&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Grouping APIs with Tags
&lt;/h2&gt;

&lt;p&gt;Tags allow you to group related API operations, which makes your API documentation cleaner and easier to navigate. For instance, in the example below, we group our APIs into two categories: Retrieve Payments and Initiate Payments. Tags defined at the root level can be used for specific APIs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tags:&lt;br&gt;
  -name: _Retrieve Payments_&lt;br&gt;
    description: APIs to retrieve payment details&lt;br&gt;
  -name: Initiate Payments&lt;br&gt;
    description: APIs to initiate new credit transfers&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using defined tag for specific API:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;paths:&lt;br&gt;
  /payment/status:&lt;br&gt;
    get:&lt;br&gt;
      operationId: getAllPaymentsStatus&lt;br&gt;
      _tags:&lt;br&gt;
        - Retrieve Payments_&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Methods, Requests, and Responses
&lt;/h2&gt;

&lt;p&gt;In OpenAPI, each path can have multiple methods, such as GET, POST, PUT, etc. Each method can define its own request body, parameters, and responses. You can also use the operationId to uniquely identify an operation, which is useful when generating code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;paths:&lt;br&gt;
  /payment/status:&lt;br&gt;
    get:&lt;br&gt;
      operationId: getAllPaymentsStatus&lt;br&gt;
      tags:&lt;br&gt;
        - Retrieve Payments&lt;br&gt;
      summary: Retrieve all payments&lt;br&gt;
      description: Returns the list of payments initiated with their status&lt;br&gt;
      responses:&lt;br&gt;
        '200':&lt;br&gt;
          description: Returns the list of payments&lt;br&gt;
          content:&lt;br&gt;
            application/json:&lt;br&gt;
              schema:&lt;br&gt;
                type: array&lt;br&gt;
                items:&lt;br&gt;
                  type: object&lt;br&gt;
                  properties:&lt;br&gt;
                    paymentId:&lt;br&gt;
                      type: integer&lt;br&gt;
                    paymentStatus:&lt;br&gt;
                      type: string&lt;br&gt;
              examples:&lt;br&gt;
                example:&lt;br&gt;
                  value:&lt;br&gt;
                    - paymentId: 101&lt;br&gt;
                      paymentStatus: ACSC&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Components for Reusability
&lt;/h2&gt;

&lt;p&gt;The components section in OpenAPI allows you to define reusable parameters, request bodies, responses, and schemas. This is especially useful when you have common elements across multiple APIs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;components:&lt;br&gt;
  schemas:&lt;br&gt;
    paymentDetailsSchema:&lt;br&gt;
      type: object&lt;br&gt;
      properties:&lt;br&gt;
        paymentId:&lt;br&gt;
          type: number&lt;br&gt;
        amount:&lt;br&gt;
          type: number&lt;br&gt;
          format: float&lt;br&gt;
        currency:&lt;br&gt;
          type: string&lt;br&gt;
        creationDate:&lt;br&gt;
          type: string&lt;br&gt;
          format: date-time&lt;br&gt;
      required:&lt;br&gt;
        - paymentId&lt;br&gt;
        - amount&lt;br&gt;
        - currency&lt;br&gt;
        - creationDate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This schema can then be referenced in multiple places within the API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance and Polymorphism
&lt;/h2&gt;

&lt;p&gt;OpenAPI supports inheritance using the allOf keyword and polymorphism with oneOf and anyOf. This allows you to create complex data structures that share common properties.&lt;/p&gt;

&lt;p&gt;For example, a schema with polymorphism:&lt;br&gt;
&lt;code&gt;components:&lt;br&gt;
  schemas:&lt;br&gt;
    accountDetails:&lt;br&gt;
      oneOf:&lt;br&gt;
        - $ref: '#/components/schemas/bankTransferDetailsSchema'&lt;br&gt;
        - $ref: '#/components/schemas/walletTransferDetailsSchema'&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Linking APIs
&lt;/h2&gt;

&lt;p&gt;When one API's output can be used as another API's input, you can define links in the OpenAPI Specification. This is useful for creating more dynamic and interconnected API workflows.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;components:&lt;br&gt;
  links:&lt;br&gt;
    LinkName_GetSinglePaymentStatus:&lt;br&gt;
      operationId: getSinglePaymentStatus&lt;br&gt;
      parameters:&lt;br&gt;
        paymentId: $response.body#/paymentId&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating Code from OpenAPI Specification
&lt;/h2&gt;

&lt;p&gt;One of the powerful features of OpenAPI is the ability to generate code automatically. Using tools like Swagger, you can generate both client and server code using options "Generate Server" or "Generate Client" available on &lt;a href="https://editor.swagger.io/" rel="noopener noreferrer"&gt;https://editor.swagger.io/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code-First Approach
&lt;/h2&gt;

&lt;p&gt;In addition to writing the OpenAPI specification first, you can also take a "code-first" approach. In this method, you start by coding your APIs and then use tools to generate the OpenAPI specification based on your code. This approach can be beneficial if you prefer to focus on coding and let the documentation follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;OpenAPI Specification is a powerful tool in your API development toolkit. Whether you take a specification-first or code-first approach, OpenAPI helps ensure your APIs are well-documented, consistent, and easy to integrate.&lt;/p&gt;

&lt;p&gt;For more information and to see a sample project, check out the &lt;a href="https://github.com/venkateshrepal/openAPI/tree/main" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any questions or thoughts, please drop a comment below. I’d love to hear your feedback.&lt;/p&gt;

</description>
      <category>openapi</category>
      <category>swagger</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
