Introduction

When building APIs using express, developers often invest time in tasks like type safety and data validation, requiring custom middleware. While this approach offers flexibility, it can lead to boilerplate code.

tRPC, an opinionated RPC alternative, simplifies development by enforcing type safety, validation, and error handling. However, its structured nature may limit customization and adaptability to unique project needs. Choosing tRPC requires assessing the balance between convenience and flexibility to ensure it aligns with your development goals.

ts-rest is a new typescript first library for making schemas or contracts for your API endpoints, providing type safety, and runtime data validation using Zod. These contracts can be consumed on server side, or client side providing utmost flexibility. The important thing to note here is that ts-rest itself is not a web framework, hence we are free to use express, or any other framework of our choosing.

Who is this for?

If you want to learn how to make backends, with secure endpoints having type-safety, and runtime data validation then this tutorial is for you.

What you will learn?

In this tutorial series, you will learn to make a simple blog REST API using ts-rest, and express. Here’s a basic outline of the different things you will learn:

  1. Defining schemas/contracts for our endpoints
  2. Data modelling, and database operations using Prisma
  3. Data validation, and type safety using Zod, and ts-rest
  4. Basic backend logic explained easily
  5. Basic User Authentication middleware
  6. Using the contract client side
  7. Making a swagger ui for our API
  8. API Versioning

Prerequisites

This tutorial assumes you have a basic understanding of Javascript, typescript, data validation using Zod and a high level understanding of web frameworks like express.

Reference

The entire project is available on GitHub here.