Concepts
What is RPC? What mindset should I adopt?
It's just functions
RPC is short for "Remote Procedure Call". It is a way of calling functions on one computer (the server) from another computer (the client). With traditional HTTP/REST APIs, you call a URL and get a response. With RPC, you call a function and get a response.
ts
// HTTP/RESTconst res = await fetch('/api/users/1');const user = await res.json();// RPCconst user = await api.users.getById({ id: 1 });
ts
// HTTP/RESTconst res = await fetch('/api/users/1');const user = await res.json();// RPCconst user = await api.users.getById({ id: 1 });
tRPC (TypeScript Remote Procedure Call) is one implementation of RPC, designed for TypeScript monorepos. It has its own flavor, but is RPC at its heart.
Don't think about HTTP/REST implementation details
If you inspect the network traffic of a tRPC app, you'll see that it's fairly standard HTTP requests and responses, but you don't need to think about the implementation details while writing your application code. You call functions, and tRPC takes care of everything else. You should ignore details like HTTP Verbs, since they carry meaning in REST APIs, but in RPC form part of your function names instead, for instance: getUser(id)
instead of GET /users/:id
.
Vocabulary
Below are some terms that are used frequently in the tRPC ecosystem. We'll be using these throughout the documentation, so it's good to get familiar with them. Most of these concepts also have their own pages in the documentation.
Term | Description |
---|---|
Procedure ↗ | API endpoint - can be a query, mutation, or subscription. |
Query | A procedure that gets some data. |
Mutation | A procedure that creates, updates, or deletes some data. |
Subscription ↗ | A procedure that creates a persistent connection and listens to changes. |
Router ↗ | A collection of procedures (and/or other routers) under a shared namespace. |
Context ↗ | Stuff that every procedure can access. Commonly used for things like session state and database connections. |
Middleware ↗ | A function that can run code before and after a procedure. Can modify context. |
Validation ↗ | "Does this input data contain the right stuff?" |