# Automated SDK Generation source: https://developer.mastercard.com/blog/automated-sdk-generation/index.md Authors: Bryan O'Sullivan. The Mastercard Developers platform acts as the front door to dozens of Mastercard APIs, so when we started looking into the idea of offering SDKs to developers we knew we had to overcome several challenges: We needed to be able to onboard Mastercard services without placing too much of a burden on the product teams responsible for those services. We wanted to make it as easy as possible for developers to embed our SDKs into their own applications. Providing a great Developer Experience was key: by offering simple-to-embed SDKs with a consistent look-and-feel, developers would be more likely to engage with the platform and try out new services. Last but not least, we needed a way of generating SDKs quickly across a variety of programming languages. For obvious reasons, hand-coding our SDKs was out of the question. Along came Swagger... We did our research and talked to some of our own API developers with experience in SDK development. Based on our requirements, it quickly became clear that Swagger specification provided the richest ecosystem. In a nutshell, the Swagger toolset helps developers to design, document, and consume APIs. Using Swagger, our workflow for building and consuming service SDKs could be broken down as follows: How do our product teams document their APIs? Mastercard's product teams document their API using the OpenAPI Specification (formerly known as Swagger Specification). This is a definition format for describing an API: from a high-level service description right down to the data types returned to the consumer. For illustration purposes I'll refer to a simple OpenAPI specification throughout this post which describes a fictional blogging service. The full specification document can be found here. An OpenAPI definition is typically written as a YAML document (although JSON is also supported), and consists of three main parts: 1. Metadata This section contains information about the API itself such as licensing and contact information, a link to the terms of service, and the URL for the server hosting the API. The example below shows a Swagger definition for our simple blogging service: Swagger swagger: "2.0" info: description: "This is a simple blog service to demonstrate Swagger." version: "1.0.0" title: "My Blog Service" termsOfService: "https://my-blog-service.com/terms" contact: name: "API Support" url: "https://my-blog-service.com/support" email: "support@my-blog-service.com" host: "localhost:8080" basePath: "/my-blog-service/v1" tags: - name: "blogpost" description: "Access to blog posts" schemes: - "http" 2. Paths The Paths Object lists the API's endpoints and describes each of the operations available on those endpoints, the security mechanisms in place, what to include in the request, and the type of response you will get back. The snippet below shows how the 'get' HTTP verb is used to retrieve a blog post identified by it's ID: Swagger /blog/{id}: get: tags: - "blogpost" summary: "Find blog post by ID" description: "Returns a single blog post" operationId: "getBlogPostById" produces: - "application/json" parameters: - name: "id" in: "path" description: "ID of blog post to return" required: true type: "integer" format: "int64" responses: 200: description: "successful operation" schema: $ref: "#/definitions/BlogPost" 3. Model Definitions This section describes the types of objects that the API accepts in its request payload and/or returns in its response. In addition to giving the type for each field in the object, you can optionally give an example value. This example value can be used by Swagger UI and other tools to quickly help developers test out real values against the API. The example below shows the definition for a simple BlogPost object which contains an optional ID, a title, and a body: Swagger definitions: BlogPost: type: "object" required: - "title" - "body" properties: id: type: "integer" format: "int64" example: 1 title: type: "string" example: "My Blog Post" body: type: "string" example: "This is my blog about Swagger" How do we generate our SDKs? The Mastercard Developers team take the provided Swagger definition and build and publish service SDKs for six of the main programming languages: Java, NodeJS, PHP, Python, Ruby, and C#. This is achieved using Swagger Codegen; an open-source code generator which takes an OpenAPI Specification as input, and is capable of building SDKs across a wide variety of languages. Using Swagger Codegen, the command to build the Java client code for our fictional blogging service is as simple as: swagger-codegen generate --input-spec my-blog-service.yaml --lang java Not only will the above command create all of the code you need for a fully functional SDK, it will also generate a README to get your users up and running quickly. If you don't like how some files are formatted you can leverage the Mustache templating framework to customize them. So... job done, right? Not quite. Although Swagger Codegen is brilliant at generating a working API client with minimum fuss, there were a couple of issues which quickly became apparent. Thankfully, Swagger Codegen is extremely flexible so neither issue became a deal-breaker for us. 1. Issue When you generate an SDK with Swagger Codegen you get a whole bunch of collateral which includes a HTTP client, and various pieces of code to handle authentication and response parsing. This is perfect when you are only generating a single SDK, but it becomes wasteful, and potentially introduces versioning issues, when our customers are integrating into a number of our SDKs. Solution We bundled all of the common functionality such as HTTP client, exception handling, response marshalling etc. into language-specific core components. Running Swagger Codegen for each individual SDK would now generate only the code particular to that API, depending on the core component for everything else. This approach allows us to easily patch the core component, without requiring every one of our consumers to upgrade their SDK version. 2. Issue Running Swagger Codegen will generate a test stub for each of the operations described in your OpenAPI definition. However, we needed to be able to generate actual test cases which would be capable of executing requests against our Sandbox environment and verifying that the responses were correct. Solution We were able to make use of Swagger's concept of Vendor Extensions. This allowed us to extend the OpenAPI Specification so that product teams could specify sample requests and responses in their Swagger definition. Once we patched our generator to understand this extension, we were in a position to generate and publish our SDKs with the confidence that they would work as expected against a real service. Over time we enhanced this functionality to allow for more complex testing scenarios. For example you may have one test which creates a resource via a POST and another test which updates it via a PUT. By saving the response from the first test we can inject values such as the resource ID into the request generated in the second test. Try it out Feel free to check out our sample project on GitHub and run the commands in the README to generate an SDK. Swagger Codegen can also be used to generate server stubs in a number of languages. These server stubs come with Swagger UI so play around with the YAML file and see how your changes are reflected in both your SDK and Swagger UI.