ts-rest
Incrementally adoptable RPC-like client and server helpers for a magical end to end typed experience 🪄
Join us on Discord for help, feedback, and discussions!
Introduction
ts-rest offers a simple way to define a contract for your API, which can be both consumed and implemented by your application, giving you end to end type safety without the hassle or code generation.
Features
- End-to-end type safety 🛟
- RPC-like client side API ⚡️
- Small Bundle Size 📉
- No Code Generation 🏃♀️
- Zod support for runtime validation 🔒
- Full optional OpenAPI integration 📝
👉 Start reading the official Quickstart Guide 👈
Super Simple Example
Easily define your API contract somewhere shared
const contract = c.router({
getPosts: {
method: 'GET',
path: '/posts',
query: z.object({
skip: z.number(),
take: z.number(),
}), // <-- Zod schema
responses: {
200: c.type<Post[]>(), // <-- OR normal TS types
},
headers: z.object({
'x-pagination-page': z.coerce.number().optional(),
}),
},
});
Fulfill the contract on your server, with a type-safe router:
const router = s.router(contract, {
getPosts: async ({ params: { id } }) => {
return {
status: 200,
body: prisma.post.findUnique({ where: { id } }),
};
},
});
Consume the api on the client with a RPC-like interface:
const result = await client.getPosts({
headers: { 'x-pagination-page': 1 },
query: { skip: 0, take: 10 },
// ^-- Fully typed!
});
Quickstart
Create a contract, implement it on your server then consume it in your client. Incrementally adopt, trial it with your team, then get shipping faster.
👉 Start reading the official Quickstart Guide 👈
Contributors ✨
MASSIVE Thanks to all of these wonderful people (emoji key), who have helped make ts-rest possible:
Youssef Gaber 💻 🤔 ⚠️ |
Per Hermansson 📖 💻 |
Grégory Houllier 📖 |
Michael Angelo 📖 |
Pieter Venter 📖 |
Rifaldhi AW 📖 |
Jonathan White 💻 📖 |
Max Brosnahan 💻 🤔 |
Oliver Butler 💻 🤔 📖 🚇 🚧 |
Adrian Barylski 💻 📖 ⚠️ |
Neil A. Dobson 💻 |
Eric Do 📖 |
Ben 💻 📖 readme truncated — read the full docs on github Frequently asked questionsIs ts-rest free to use?ts-rest is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead. What does ts-rest do?RPC-like client, contract, and server implementation for a pure REST API What is ts-rest written in?ts-rest is primarily written in TypeScript. Its source is publicly available at https://github.com/ts-rest/ts-rest, and it has 3,337 GitHub stars. |