API with NestJS #116. REST API versioning

NestJS

This entry is part 116 of 168 in the API with NestJS

The requirements of web applications constantly evolve, and so do the REST APIs they use. With the rise of the popularity of distributed systems and microservices, maintaining backward compatibility is increasingly important. Even if we maintain a monolithic architecture, there is a good chance that our frontend has a separate deployment pipeline. In this article, we implement REST API versioning in our NestJS application to handle updates gracefully and maintain a stable system.

Check out this repository to see the code from this article.

Versioning with NestJS

In one of the previous parts of this series, we changed the property of the posts to with a SQL migration.

If you want to know more about migrations with Prisma, check out API with NestJS #115. Database migrations with Prisma
If you use TypeORM, take a look at API with NestJS #69. Database migrations with TypeORM

Because of the above change, creating a new version of the endpoints related to posts makes sense. By implementing REST API versioning, we can maintain multiple versions of our controllers or individual routes. The most straightforward approach to API versioning is to include the version number in the URI of the request.

Adding versioning to an existing project

To start working with versions, we need to modify our function.

main.ts

is the default value for the versioning type but we can provide it for the sake of readability.

Due to how we configured versioning with the method, our existing routes don’t include the version in the URL yet. Thanks to this approach, we are not introducing any breaking changes when starting to use versioning.

We now need to pass an additional argument to the decorator that specifies the version.

posts.controller.ts

Thanks to doing the above, we now have the prefix in the URLs of endpoints related to posts.

To create a different version of the endpoints, we need to define a separate controller.

postsLegacy.controller.ts

By using as the version, NestJS uses the legacy controller when the version is not specified in the URL. Thanks to this approach, we can achieve backward compatibility if our project didn’t use versioning before.

We also need to remember about adding our new controller to the module.

posts.module.ts

Adjusting our controller to the new schema

We now need to deal with the legacy API handling data using the old format with instead of . In one of the previous parts of this series, we learned how to serialize the response of our controller through a custom interceptor when using Prisma. To use it, we need to create a DTO class first.

postsResponseLegacy.dto.ts

Above, we hide the property that came from our database and create the . Thanks to this, we can serve our data in a legacy format, even though the database stores it differently.

postsLegacy.controller.ts

We can use a similar approach when creating entities by transforming the data posted by the user.

createPostLegacy.dto.ts

We now need to use the above class in our legacy controller.

postsLegacy.controller.ts

Designing the API with versioning in mind

There is a high probability that we will need to add versioning to our application at some point. Since that’s the case, we might as well design it with versioning in mind.

main.ts

By supplying to the method, we add the prefix to all our endpoints. Now, whenever we create a new version for one of the controllers, the change in the URL feels more natural. For example, becomes .

The above also requires us to adjust our legacy controller. Instead of , we specify as the version.

postLegacy.controller.ts

Other types of versioning

While the URI versioning is default and very popular, NestJS also supports other types of versioning.

Header versioning

The header versioning uses a custom header to specify the version of the controller used to handle a particular request.

Media type versioning

The request header indicates which content types the API client can understand. A good example is .

With media-type versioning, we use the header to specify the version, such as .

Custom versioning type

If none of the above solutions fit our API, we can implement a custom versioning.

Unfortunately, the type of the is and we need to use type assertions.

Summary

In this article, we’ve gone through the idea of REST API versioning. We used URI versioning to create a legacy version of our controller and transformed data to match the previous format our API was using. We also learned about other versioning types, such as header and media-type versioning.

API versioning can be a handy tool to preserve backward compatibility. However, we need to remember to remove the older versions of our API when they are not required anymore to avoid cluttering our codebase.

Series Navigation<< API with NestJS #115. Database migrations with PrismaAPI with NestJS #117. CORS – Cross-Origin Resource Sharing >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments