How to generate API client library

In the previous article Getting started with Web API, we have created an API with Swagger. Swagger allows you to see a documentation of existing calls and provides an interface where they can be tested quickly. Moreover, a specification of a WebAPI can be used to generate a client library.

Generate client

When our API is up and running, we can start to generate one or more clients for different types of frameworks and programming languages.

 

  1. Go to https://generator.swagger.io/#!/clients/generateClient
  2. Navigate to “clients” controller and toggle /gen/clients/{language}”  POST operation. 
  3. Click “Try it out”.
  4. Select language of the desired client library. I’ll go with “csharp” for C#.
  5. Update request body with following JSON.
    • Specify package name for your API library project in “packageName” property.
    • Update url set in “swaggerUrl” parameter to your own swagger documentation. It can be accessed by adding “swagger/docs/{version}” to your API url.

  6. Click “Execute”.
  7. Parse “link” property value from response body and download archive with your client.
  8. Extract the archive and open its solution.
  9. Build project.

Conclusion

Now, it’s up to you if you use “.dll”  file or just reference the project in your application.

Hope it helps.

Leave a Reply