Search results

Introduction to the OpenAPI specification and Swagger

Keep current with the latest trends in technical communication by subscribing to the I'd Rather Be Writing newsletter. 4,500+ subscribers

OpenAPI is a specification for describing REST APIs. You can think of the OpenAPI specification like the specification for DITA. With DITA, there are specific XML elements used to define help components, and a required order and hierarchy to those elements. Different tools can read DITA and build out a documentation website from the information.

With OpenAPI, instead of XML, you have set of JSON objects, with a specific schema that defines their naming, order, and contents. This JSON file (often expressed in YAML instead of JSON) describes each part of your API. By describing your API in a standard format, publishing tools can programmatically parse the information about your API and display each component in a stylized, interactive display.

If you want to jump straight into a step-by-step tutorial for creating the OpenAPI specification document, see the OpenAPI tutorial overview.

Glancing at the OpenAPI specification

To get a better sense of the OpenAPI specification, let’s take a quick glance at some specification excerpts. We’ll dive deeper into each element in an upcoming tutorial.

The official description of the OpenAPI specification is available in a Github repository here. Some of the OpenAPI elements are paths, parameters, responses, and security. Each of these elements is a JSON object that holds a number of properties and arrays.

In the OpenAPI specification, your endpoints are paths. If you had an endpoint called “pets”, your OpenAPI specification for this endpoint might look as follows:

paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: An paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"

This YAML code comes from the Swagger Petstore demo.

Here’s what these objects mean:

  • /pets is the endpoint path.
  • get is the HTTP method.
  • parameters lists the parameters for the endpoint.
  • responses lists the response from the request.
  • 200 is the HTTP status code.
  • $ref is actually a reference to another part of your implementation where the response is defined (in components). OpenAPI has a lot of $ref references like this to keep your code clean and to facilitate re-use.

It can take quite a while to figure out the OpenAPI specification. Give yourself a couple of weeks and a lot of example specification documents to look at, especially in the context of the actual API you’re documenting. Remember that the OpenAPI specification is general enough to describe nearly every REST API, so some parts may be more applicable than others.

Validating the specification

When you’re coding your OpenAPI specification document, instead of working in a text editor, you can write your code in the Swagger Editor. The Swagger Editor dynamically validates your content to determine whether the specification document you’re creating is valid.

The Swagger Editor validates your specification content dynamically and shows you the display on the right
The Swagger Editor validates your specification content dynamically and shows you the display on the right

While you’re coding in the Swagger Editor, if you make an error, you can quickly fix it before continuing, rather than waiting until a later time to run a build and sort out errors.

For your specification document’s format, you have the choice of working in either JSON or YAML. The previous code sample is in YAML. YAML refers to “YAML Ain’t Markup Language,” meaning YAML doesn’t have any markup tags (< >), as would be common with other markup languages such as XML.

YAML depends on spacing and colons to establish the object syntax. This makes the code more human-readable, but it’s also sometimes trickier to get the spacing right.

Auto-generating the OpenAPI file from code annotations

Instead of coding the OpenAPI specification document by hand, you can also auto-generate it from annotations in your programming code. This developer-centric approach may make sense if you have a large number of APIs or if it’s not practical for technical writers to create this documentation.

Swagger offers a variety of libraries that you can add to your programming code to generate the specification document. These Swagger libraries then parse the annotations that developers add and generate the OpenAPI specification document. These libraries are considered part of the Swagger Codegen project. The annotation methods vary based on the programming language. For example, here’s a tutorial on annotating code with Swagger for Scalatra. For more information on Codegen, see Comparison of Automatic API Code Generation Tools For Swagger by API Evangelist. For additional tools and libraries, see Swagger services and tools and Open Source Integrations.

Although this approach “automates” the spec’s generation, someone still has to know exactly what annotations to add and how to add them (the process isn’t too unlike Javadoc’s comments and annotations). Then someone has to write content for each of the annotation’s values (describing the endpoint, the parameters, and so on).

In short, this process isn’t without effort — the automated part is having the Codegen libraries generate the model definitions and the valid specification document that conforms the OpenAPI schema. Still, many developers get excited about this approach because it offers a way to generate documentation from code annotations, which is what developers have been doing for years with other programming languages such as Java (using Javadoc) or C++ (using Doxygen). They usually feel that generating documentation from the code results in less documentation drift. Docs are likely to remain up to date if the doc is tightly coupled with the code.

If you go this route, make sure you get access to the source code to make edits to the annotations. Otherwise, your developers will be writing your docs (which can be good but often leads to poor results).

Another approach: spec-first development

Although you can generate your specification document from code annotations, many say that auto-generation is not the best approach. In Undisturbed REST: A Guide to Designing the Perfect API, Michael Stowe recommends that teams implement the specification by hand and then treat the specification document as a contract that developers use when doing the actual coding. This approach is often referred to as “spec-first development.”

Spec-first development
Spec-first development is a philosophy about how to develop APIs in a more efficient way. If you follow a spec-first philosophy, you write the spec first and use it as a contract that developers code to.

In other words, developers consult the specification document to see what the parameter names should be called, what the responses should be, and so on. After this “contract” or “blueprint” has been established, Stowe says you can then put the annotations in your code (if you want) to generate the specification document in a more automated way. But don’t code without first having a spec.

Too often, development teams quickly jump to coding the API endpoints, parameters, and responses without doing much user testing or research into whether the API aligns with what users want. Since versioning APIs is extremely difficult (you have to support each new version going forward with full backwards compatibility to previous versions), you want to avoid the “fail fast” approach that is so commonly celebrated with agile. There’s nothing worse than releasing a new version of your API that invalidates endpoints or parameters used in previous releases. Constant versioning in APIs can become a documentation nightmare.

In my conversations with Smartbear, the company that makes SwaggerHub (a collaborative platform for teams to work on Swagger API specifications), they say it’s now more common for teams to manually write the spec rather than embed source annotations in programming code to auto-generate the spec. The spec-first approach helps distribute the documentation work to more team members than engineers. Defining the spec before coding also helps teams produce better APIs.

Even before the API has been coded, your spec can generate a mock response by adding response definitions in your spec. The mock server generates a response that looks like it’s coming from a real server, but it’s really just a pre-defined response in your code and appears to be dynamic to the user.

The tech writer’s role with the spec

In most of my projects, developers haven’t been that familiar with Swagger or OpenAPI, so I usually create the OpenAPI specification document by hand. Additionally, I often don’t have access to the programming source code, and our developers speak English as a second or third language only. They aren’t eager to be in the documentation business.

You will most likely find that engineers in your company aren’t familiar with Swagger or OpenAPI but are interested in using it as an approach to API documentation (the schema-based approach fits the engineering mindset). As such, you’ll probably need to take the lead to guide engineers in the needed information, approach, and other details that align with best practices toward creating the spec.

In this regard, tech writers have a key role to play in collaborating with the API team in producing the spec. If you’re following a spec-first development philosophy, this leading role can help you shape the API before it gets coded and locked down. This means you might be able to actually influence the names of the endpoints, the consistency and patterns, simplicity, and other factors that go into the design of an API (which tech writers are usually absent from).

Rendering Your OpenAPI specification with Swagger UI

After you have a valid OpenAPI specification document that describes your API, you can then feed this specification to different tools to parse it and generate the interactive documentation similar to the Petstore demo.

Probably the most common tool used to parse the OpenAPI specification is Swagger UI. (Remember, “Swagger” refers to API tooling, whereas “OpenAPI” refers to the vendor-neutral, tool agnostic specification.) After you download Swagger UI, it’s pretty easy to configure it with your own specification file. I provide a Swagger UI tutorial in an upcoming section.

The Swagger UI code generates a display that looks like this:

The Swagger Petstore demo shows how Swagger UI renders the OpenAPI spec
The Swagger Petstore demo shows how Swagger UI renders the OpenAPI spec

You can also check out the sample Swagger UI integration with a simple weather API used as a course example.

Some designers criticize Swagger UI’s expandable/collapsible output as being dated. At the same time, developers find the one-page model attractive and like the ability to zoom out or in for details. By consolidating all endpoints on the same page in one view, users can take in the whole API at a glance. This display gives users a glimpse of the whole, which helps reduce complexity and enables them to get started. In many ways, the Swagger UI display is a quick-reference guide for your API.

Activity: Explore Swagger UI through the Petstore Demo

Let’s get some hands-on experience with Swagger UI using the Petstore demo.

  1. Go to the Swagger Pet Store Demo.

    As with most Swagger-based outputs, Swagger UI provides a “Try it out” button. To make it work, first you would normally authorize Swagger by clicking Authorize and completing the right information required in the Authorization modal. If you want, in the Authorization modal, type any number in the api_key field and click Authorize. However, the Petstore authorization modal is just for demo purposes. There isn’t any real code authorizing those requests, so you can simply close the Authorization modal.

    Authorization modal in Swagger UI
    Authorization modal in Swagger UI
  2. Expand the Pet endpoint.
  3. Click Try it out. After you click Try it out, the example value in the Request Body field becomes editable.

    Try it out button in Swagger UI
    Try it out button in Swagger UI
  4. In the Example Value, change the first id value to a unique (and unlikely repeated) whole number. Change the second name value to something you’d recognize (your pet’s name — e.g., Bentley).
  5. Click Execute.

    Executing a sample Petstore request
    Executing a sample Petstore request

    Swagger UI submits the request and shows the curl that was submitted. For example, here’s the curl Swagger UI sent:

    curl -X POST "https://petstore.swagger.io/v2/pet" -H "accept: application/xml" -H "Content-Type: application/json" -d "{ \"id\": 1000, \"category\": { \"id\": 0, \"name\": \"string\" }, \"name\": \"Bentley\", \"photoUrls\": [ \"string\" ], \"tags\": [ { \"id\": 0, \"name\": \"string\" } ], \"status\": \"available\"}"
    

    Notice that, with the -d (data) parameter, the request body parameter is escaped and added directly into the curl command rather than being loaded from a file (as explained in Common curl commands related to REST).

    The Responses section in Swagger UI shows the response from the server. By default, the response returns XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <Pet>
        <category>
          <id>0</id>
          <name>string</name>
        </category>
        <id>1000</id>
        <name>Bentley</name>
        <photoUrls>
          <photoUrl>string</photoUrl>
        </photoUrls>
        <status>available</status>
        <tags>
          <tag>
            <id>0</id>
            <name>string</name>
          </tag>
        </tags>
      </Pet>
    

    If you select JSON rather than XML in the “Response content type” drop-down box, you can specify that JSON is returned rather than XML.

    Response from Swagger Petstore get pet request

  6. The Petstore is a functioning API, and you have actually created a pet. For fun, expand the GET /pet/{petId} endpoint, click Try it out, enter the pet id you used in the previous operation, and then execute the request. You should see your pet’s name returned.

Other tools for reading OpenAPI spec

There are other tools besides Swagger UI that can parse your OpenAPI specification document. Some of these tools include Restlet Studio, Apiary, Apigee, Lucybot, Gelato, Readme.io, swagger2postman, swagger-ui responsive theme, Postman Run Buttons and more.

Some web designers have created integrations of OpenAPI with static site generators such as Jekyll (see Carte and Readme). You can also embed Swagger UI into web pages as well. More tools roll out regularly for parsing and displaying content from a OpenAPI specification document.

In fact, once you have a valid OpenAPI specification, using a tool called API Transformer, you can even transform it into other API specification formats, such as RAML or API Blueprint. This allows you to expand your tool horizons even wider.

Customizing Swagger UI

You might be concerned that Swagger UI outputs look similar. With my OpenAPI projects, I usually customize the Swagger UI’s colors a bit, add a custom logo and a few other custom styles. With one project, I spliced in a reference to Bootstrap so that I could have pop-up modals where users could generate their authorization codes. You can even add collapse and expand features in the description element to provide more information to users.

Beyond these simple modifications, however, it takes a bit of web-developer prowess to significantly alter the Swagger UI display. It’s possible, but you need web development skills.

Downsides to OpenAPI and Swagger UI

Despite Swagger’s interactive power to appeal to the “let me try” desires of users, there are some downsides to Swagger and OpenAPI:

  • Documentation for reference information only: First, the OpenAPI specification and Swagger UI’s output cover only reference documentation. OpenAPI provides the basics about each endpoint, including a description, the parameters, a sample request, and a response. It doesn’t provide space for a getting started tutorial, information about how to get API keys, how to run a sample app, information about rate limits, or the hundred other details that go into a user guide for developers. So even though you have this cool, interactive tool for users to explore and learn about your API, you still have to provide a user guide. As an analogy, delivering a Javadoc or Doxygen output for a library-based API won’t teach users how to actually use your API. You still have to describe scenarios for using a class or method, explain how to set your code up, what to do with the response, how to troubleshoot problems, and so on. In short, you still have to write actual help guides and tutorials.

  • Redundancy/duplication of information: With OpenAPI in the mix, you potentially have two places where you’re describing your endpoints and parameters (both the Swagger UI reference output and your user guide), and you have to either keep the two in sync, embed one in the other, or otherwise link between the two. I explore integration strategies in Integrating Swagger UI with the rest of your docs.

  • Complexity of API workflows: The complexity of your API can also create a limitation with Swagger. Peter Gruenbaum, who has published several tutorials on writing API documentation on Udemy, says that automated tools such as Swagger work best when the APIs are simple. When you have endpoints that have complex interdependencies and require special setup workflows or other unintuitive treatment, the straightforward nature of Swagger’s Try-it-out interface may likely leave users scratching their heads. For example, if you must first configure an API service before an endpoint returns anything, and then use one endpoint to get a certain object that you pass into the parameters of another endpoint, and so on, the Try-it-out features in the Swagger UI output won’t make a lot of sense to users without a detailed tutorial to follow.

  • Executing requests against real data: Some users may not realize that clicking “Try it out” makes actual calls against their own accounts based on the API keys they’re using. Mixing an invitation to use an exploratory sandbox like Swagger with real data can create some headaches later on when users ask how they can remove all of the test data, or why their actual data is now messed up. For these scenarios, it’s best to set up a sandbox or test account for users. But this is easier said than done. You might find that your company doesn’t provide a sandbox for testing out the API. All API calls execute against real data.

  • CORS restrictions: You might run up against CORS (Cross-Origin Resource Sharing) restrictions in executing API calls. Not all APIs will accept requests executed from a web page. If the calls aren’t executing, open the JavaScript Console and check whether CORS is blocking the request. If so, you’ll need to ask developers to make adjustments to accommodate requests initiated from JavaScript on web pages. See CORS Support for more details.

  • Extensive request body parameters problematic: Finally, I found that endpoints with lengthy request body parameters tend to be problematic. One API I had to document included requests with request body parameters that were hundreds of lines long (the request body was used to configure an API server). With this sort of request body parameter, Swagger UI’s display fell short of being usable. The team reverted to much more primitive approaches (such as tables and Excel spreadsheets) for listing all of the parameters and their descriptions.

Some consolations

Despite the shortcomings of OpenAPI, I still highly recommend it for describing your API. OpenAPI is quickly becoming a way for more and more tools (from Postman Run buttons to nearly every API platform) to easily ingest the information about your API and make it discoverable and interactive with robust, instructive tooling. Through your OpenAPI specification, you can port your API onto many platforms and systems as well as automatically set up unit testing and prototyping.

Swagger UI definitely provides a nice visual shape for an API. You can easily see all the endpoints and their parameters (like a quick-reference guide). Based on this framework, you can help users grasp the basics of your API.

Additionally, I found that learning the OpenAPI specification and describing my API with these objects and properties helped inform my own API vocabulary. For example, I realized that there were four main types of parameters: “path” parameters, “header” parameters, “query” parameters, and “request body” parameters. I learned that parameter data types with REST were a “Boolean”, “number”, “integer”, or “string.” I learned that responses provided “objects” containing “strings” or “arrays.”

In short, implementing the specification gave me an education about API terminology, which in turn helped me describe the various components of my API in credible ways.

OpenAPI may not be the right approach for every API, but if your API has fairly simple parameters, without many interdependencies between endpoints, and if it’s practical to explore the API without making the user’s data problematic, OpenAPI and Swagger UI can be a powerful complement to your documentation. You can give users the ability to try out requests and responses for themselves.

With this interactive element, your documentation becomes more than just information. Through OpenAPI and Swagger UI, you create a space for users to both read your documentation and experiment with your API at the same time. That combination tends to provide a powerful learning experience for users.

Resources and further reading

See the following resources for more information on OpenAPI and Swagger:

To see a presentation that covers the same concepts in this article, see https://goo.gl/n4Hvtq.

37% Complete

37/110 pages complete. Only 73 more pages to go...

Donate?

Want to buy me lunch? Click the Donate button below to donate $10 through Paypal.