One of the benefits of basing an API on schemas is the ability to use code generators. Generated code can have some disadvantages like generating hard to understand code and difficulties in customizing a request beyond what the generated code allows. Personally, in many cases, I find manually written code to more elegant. However, for schema-based code, code generation is a tool that can quickly and consistently build good solutions.

Generated code should be consistent across all API requests and responses. Therefore, once a request is made and a response is processed, the usage knowledge should apply to all requests and responses. If there is a bug in the generated code, a fix can be consistently applied to all of the applicable code. Furthermore, if there is a need for clients in different programming languages, code generation can ensure that all languages are able to get access to the new APIs with minimal effort. In effect, a code generator can be a codification of how clients should interact with an API.

Code generation applies to more than code for APIs, but it is especially suited to API client SDKs. Client SDKs are tedious to maintain, and eventually maintaining multiple programming language bindings will be required (if not immediately, eventually). Schemas can help maintain and verify API behavior but generating code can ensure a consistency from the clients as well. When a new API needs to be exposed, instead of an engineer having to recall how to write the SDK code (and keep the code consistent with the rest of the SDK), the code generator can be given the schema and output the code.

In API SDKs, there is usually a base protocol client such as a HTTP client. The client can be coded manually and expose unique features per platform. The generated code is usually the requests, responses, and domain specific objects.

The generated code can also be generated in a way to allow custom protocol clients to use the code. Therefore, the code can still be of some value where someone wants to quickly interact with the API but wants to customize the protocol behavior. Imagine an app that has specific requirements for timeouts or an app that needs to use a specific pool of HTTP clients for outbound requests. If the generated code is not coupled with the protocol client, it can still possibly be used to serialize/deserialize the request/response.

While I’ve not had a strong preference towards code generation in the past, code generation can be a great tool, especially when API SDKs are needed.