Turn your Postman Collection into models with Quicktype.io

Avatar

For API consumers and builders alike, spend less time writing model classes and serialization code. Automatically generate them in your preferred programming language with the free and open source tool, quicktype.

Say you’re working with an API that’s not well-documented and lacks a client library in your app’s programming language. If you have sample responses in a Postman collection, you can use quicktype to generate code to load and work with that data.

On the other hand, say you’re building APIs with an API-first development approach. During the design phase, you might mock the endpoint before developing your API and model. In the same manner, you can generate model classes and helper functions from these mock responses.

So how does it work? David Siegel, co-creator of quicktype, shows us how to generate models and serializers from a Postman Collection.

quicktype generates perfect API code from Postman Collections

by David Siegel, co-creator of quicktype

David Siegel is the co-creator of quicktype along with Mark Probst. David & Mark began with the simple idea of making JSON easier to parse in typed programming languages, and are now trying to type all untyped data!

quicktype is an open source tool for automatically generating models and serializers from JSON sample data, making it a breeze to work with JSON type-safely in any programming language. Some Postman users find quicktype useful for generating code for API responses, so quicktype has added first-class support for Postman Collections. Simply pass a Postman Collection to quicktype to generate perfect API models in C#, TypeScript, Swift, C++, Go, Java, and other languages.

Here’s a brief demo of quicktype in action:

quicktype a sample collection

The Postman Echo sample collection includes a ‘leap year check’ endpoint at https://postman-echo.com/time/leap. It takes a single timestamp parameter and returns a JSON response like { "leap": true } indicating whether that time falls within a leap year:

You can export the sample collection and drop it into quicktype:

In this example, quicktype generated Swift Codables to make it easy to call the API from an iOS app:

Alamofire.request("https://postman-echo.com/time/leap").responseLeapYearCheck { response in
    if let leapYearCheck = response.result.value {
        if leapYearCheck.leap {
            print("Dude, it's a leap year ?")
        } else {
            print("Darn, it's just a regular year ?")
        }
    }
}

We didn’t need to write any other code—quicktype generated the LeapYearCheck struct and responseLeapYearCheck helper method for making this API call and marshaling its response.

This is a basic example, but it works for any collection with response samples, and for many languages besides Swift—for example, the Imgur collection can generate thousands of lines of code to represent its responses. This is not code that you want to write by hand or update manually as the Imgur API evolves.

Drop a collection into quicktype today and check out quicktype on GitHub for more details!

Tags:

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.