Adding new endpoint
How to add new endpoint to the API.
To define a new API endpoint, you can either extend an existing entity (e.g. add new customer note) or create a new, separate module.
Create new module
To create a new module you can create a new folder in the modules
folder. For example modules/posts
.
Then you would need to create a router declaration for this module. We're following a convention with the filename suffixes, so you would need to create a file named posts.router.ts
in the modules/posts
folder.
We're using publicProcedure
here, but you can also use protectedProcedure
to restrict access to the endpoint.
Maybe mutation?
The same way you can define a mutation for the new entity, just by changing the query
to mutation
:
That way you also validate if the input is correct and the user is authorized to perform this action.
Implement logic
Then you would need to create a controller for this module. There is a place, where the logic happens, e.g. for the getAll
endpoint we would need to create a getAllPosts
function which will fetch posts from the database.
Register router
To make the module and its endpoints available in the API you need to register a router for this module in the router.ts
file:
That's it! You've just created a new API endpoint.
It's fully type-safe!
By exporting the AppRouter
type you get fully type-safe RPC calls in the
client. It's important because without producing a huge amount of code, we're
fully type-safe from the frontend code. It helps avoid passing incorrect data
to the procedure and streamline consuming returned types without a need to
define these types by hand.
Last updated on