Document your API in GitBook in 5 simple steps
Learn how to build beautiful, interactive API documentation in GitBook.
Last updated
Was this helpful?
Learn how to build beautiful, interactive API documentation in GitBook.
Last updated
Was this helpful?
Was this helpful?
This comprehensive guide will walk you through the process of documenting your API in GitBook, from initial setup to advanced customization. By the end, you’ll have a professional API reference that your users will love.
GitBook’s API features allows you to create beautiful, interactive API documentation using your OpenAPI (formerly Swagger) specification. It supports:
OpenAPI 3.0 and 3.1 specifications
Multiple API versions
Custom code samples
Enums and complex data types
CI/CD integration
+ more
Before you begin, you’ll need:
Navigate to your GitBook organization
Click the OpenAPI section in the sidebar
Click Add specification
Choose one of these upload methods:
Direct upload: Upload your OpenAPI specification file
URL import: Provide a public URL to your specification
CLI: Use the CLI to publish your OpenAPI spec to GitBook
Go to the space where you want to insert your API reference
Click the + Add new... button at the bottom of the table of contents
Select Open API reference from the list
Choose your API specification
Insert an API block by pressing / or clicking the + button on an empty block and choosing OpenAPI
Select your API specification
Choose Select endpoints
Pick the specific operations you want to include
After adding your specification, GitBook will automatically generate a full API reference for your endpoints described in your OpenAPI spec.
Make sure to publish your site or merge the change request you’re working in to see your changes live on your GitBook site.
GitBook does more than just render your OpenAPI spec. It lets you customize your API reference for better clarity, navigation, and branding.
You can customize many things in your API reference, from the page titles, descriptions and icons, to the organization and grouping of your endpoints, to the code samples you display.
After setting up the structure of your API documentation, you can continue to add extra functionality and information to your endpoints to make them easier to understand or use.
Adding this Markdown to your operation’s description will add a tab block to the description in your endpoint in GitBook.
You can also add custom code samples directly in your OpenAPI specification. You can use x-code-samples
to add language specific code samples that render in your API methods.
After your endpoints are configured and structured, you and your users are ready to test your endpoints — right from your own docs!
Any endpoints added to GitBook through your OpenAPI reference are automatically added with a “Test it” button, which lets you and your users send test requests right from your docs.
You can customize parameters, add authentication tokens, and much more. Make sure you publish your docs and test out your API to make sure everything looks and is working correctly!
Now that you’ve got your API documentation up and running, it’s important to keep things consistent — consistency is key!
A GitBook account and a space in an organization
Your API specification in OpenAPI format (JSON or YAML)
A basic understanding of your API structure
With GitBook, you can customize your API reference through different options in your OpenAPI spec. You can learn more about the OpenAPI specification here.
You can organize your endpoints by adding a set of tags at the top level of your OpenAPI spec.
Adding a list of tags will allow you to describe the pages and order of your API. The example above creates three pages you can add endpoints to, called “Pet”, “Store”, and “User”.
The order of pages in GitBook matches the order of tags in your OpenAPI tags array.
After defining your tags, you can customize them further by adding titles, icons, and descriptions
The x-page-title
, x-page-icon
, and x-page-description
describe the title, icon, and description used in the GitBook page respectively.
description
allows you to add content to the top of the page, before the API endpoints show.
Want to see an example? Head to our own developer docs to see GitBook’s own API in action. You can also take a look at how we’ve structured our own OpenAPI spec to learn more about structuring an OpenAPI spec at scale.
Check our guide on the seven principles of great API documentation — it’s a great reference as your API continues to grow. And if you’re curious to learn more about OpenAPI and how to build better docs in GitBook, make sure you head to our own documentation to learn more about OpenAPI and GitBook.
If you’d like to group multiple pages together, you can use x-parent
in tags to define hierarchy:
The above example will create a table of contents that looks like this:
If a top level page has no description or content, GitBook will automatically show a card-based layout for the sub-pages within.