How to Automatically Generate Clients for your REST API
Create client code in seconds for all popular languages, and save hours of repetitive manual labor.
Chances are, if you’re developing RESTful APIs, you’ve heard of the OpenAPI specification (formerly known as Swagger) before. The OpenAPI Specification defines a standard interface to describe an API, regardless of the language it was written in. This specification can then be used to automatically create documentation based on conventions, or to automatically generate clients for your services.
Modern application landscapes often comprise of many independent services. Each of these services have one or more responsibilities, and have an interface to expose functionality to the outside world. In the case of RESTful services, this functionality is exposed over HTTP. Writing client code to interface with these services is often a very repetitive, slightly tedious task.
Fortunately, with the power of Microsoft’s AutoRest code generator, we can automatically generate client code for almost every popular language or framework out there.
In this guide, you’ll learn how to set up AutoRest to leverage the OpenAPI specification, and to generate a client for you. We’ll be using an ASP.NET Core Web API in the example code, but the process is much the same for any other language or framework.
What you’ll need:
- NodeJS — 10.15.x LTS is the preferred version at the time of writing, but any LTS version should be fine
- An API with an OpenAPI specification. You can also grab the API that’s used for this guide from GitHub and run it if you have the .NET SDK installed
Notes for the Sample API
If you decide to use the sample API to follow along with this guide, there are a couple of additional things you’ll need:
- Visual Studio or Visual Studio Code. The former comes with the .NET Core 3.1 SDK, the latter doesn’t. If needed, you can download the .NET SDK from the official website.
- By default, the API serves content on port 5000. You can change this in
Properties/launchSettings.jsonif that port doesn’t work for you.