DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Code and Connect: MCP + MuleSoft
  • Phantom APIs Are Eating Your Attack Surface, and Most Security Teams Are Still Looking the Other Way
  • How to Set MX Records via API: Automate Email Routing Programmatically
  • I Reverse-Engineered 50 API Breaches. The Same Five Mistakes Keep Appearing.

Trending

  • OpenAPI, ORM, SVG, and Lottie
  • Implementing Asynchronous Communication Between Microservices Using Kafka and Spring Boot
  • Who Owns the Data Stack?: How AI Is Reshaping Ownership, Architecture, and Accountability Across Teams
  • Building an Agentic Incident Resolution System for Developers
  1. DZone
  2. Data Engineering
  3. Databases
  4. Auto-Generating API Specifications as OpenAPI, WSDL, and Sphinx

Auto-Generating API Specifications as OpenAPI, WSDL, and Sphinx

This article presents a workflow for auto-generation of API specifications for your Zato services.

By 
Dariusz Suchojad user avatar
Dariusz Suchojad
·
Dec. 13, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
23.5K Views

Join the DZone community and get the full member experience.

Join For Free

Sphinx statue

Auto-Generating API Specifications as OpenAPI, WSDL, and Sphinx


This article presents a workflow for auto-generation of API specifications for your Zato services. If you need to share your APIs with partners, external or internal, this is how it can be done.


Sample Services


Let's consider the services below — they represent a subset of a hypothetical API of a telecommunication company. In this case, they are to do with pre-paid cards. Deploy them on your servers in a module called api.py.

Note that their implementation is omitted, we only deal with their I/O, as it is expressed using SimpleIO.

What we would like to have, and what we will achieve here, is a website with static HTML describing the services in terms of a formal API specification.

Java
 




xxxxxxxxxx
1
32


 
1
# -*- coding: utf-8 -*-
2
 
          
3
# Zato
4
from zato.server.service import Int, Service
5
 
          
6
# #####################################################################
7
 
          
8
class RechargeCard(Service):
9
    """ Recharges a pre-paid card.
10
    Amount must not be less than 1 and it cannot be greater than 10000.
11
    """
12
    class SimpleIO:
13
        input_required = 'number', Int('amount')
14
        output_required = Int('status')
15
 
          
16
    def handle(self):
17
        pass
18
 
          
19
# #####################################################################
20
 
          
21
class GetCurrentBalance(Service):
22
    """ Returns current balance of a pre-paid card.
23
    """
24
    class SimpleIO:
25
        input_required = Int('number')
26
        output_required = Int('status')
27
        output_optional = 'balance'
28
 
          
29
    def handle(self):
30
        pass
31
 
          
32
# #####################################################################



Docstrings and SimpleIO


In the sample services, observe that:


  • Documentation is added as docstrings — this is something that services, being simply Python classes, will have anyway
  •    
  •  One of the services has a multi-line docstring, whereas the other one is single-line. This will be of significance later on
  •    
  • SimpleIO definitions use both string types and integers
You might also like:  Creating an API Specification in a Design Center

Command Line Usage


To generate API specifications, command zato apispec is used. This is part of the CLI that Zato ships with.

Typically, only well-chosen services should be documented publicly, and the main two options the command has are --include and --exclude.

Both accept a comma-separated list of shell-like glob patterns that indicate which services should or should not be documented.

For instance, if the code above is saved in api.py, the command to output their API specification is:

Java
 




xxxxxxxxxx
1


 
1
zato apispec /path/to/server        \
2
    --dir /path/to/output/directory \
3
    --include api.*



Next, we can navigate to the directory just created and type the command below to build HTML.

Java
 




xxxxxxxxxx
1


 
1
cd /path/to/output/directory
2
make html



OpenAPI, WSDL, and Sphinx


The result of the commands is as below — OpenAPI and WSDL files are in the menu column to the left.

Also, note that in the main index only the very first line of a docstring is used but upon opening a sub-page for each service its full docstring is used.


Branding and Customization


While the result is self-contained and it can be already used as-is, there is still room for more.

Given that the output is generated using Sphinx, it is possible to customize it as needed, for instance, by applying custom CSS or other branding information, such as the logo of a company exposing a given API.

All of the files used for generation of HTML are stored in config directories of each server — if the path to a server is /path/to/server, then the full path to Sphinx templates is in /path/to/server/config/repo/static/sphinxdoc/apispec.


Summary


That is everything — generating static documentation is a matter of just a single command. The output can be fully customized while the resulting OpenAPI and WSDL artifacts can be given to partners to let third-parties automatically generate API clients for your Zato services.

API

Opinions expressed by DZone contributors are their own.

Related

  • Code and Connect: MCP + MuleSoft
  • Phantom APIs Are Eating Your Attack Surface, and Most Security Teams Are Still Looking the Other Way
  • How to Set MX Records via API: Automate Email Routing Programmatically
  • I Reverse-Engineered 50 API Breaches. The Same Five Mistakes Keep Appearing.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook