Use API in application
Learn how to fetch data from your API in your supastarter application.
To query this endpoint from your application in a type-safe way, you can use the apiClient
in your Next.js application.
Use API in a client component
You can use the apiClient
directly in a useEffect
for example, but we recommend using it in combination with Tanstack Query for a better developer and user experience.
It gives you a lot of features like caching, error handling, loading states, and more.
You now have a fully type-safe API client that you can use in your application.
You can find many examples of how to use API endpoints with Tanstack Query in the supastarter application code. Just search for apiClient
, useQuery
and useMutation
in the codebase.
Use API in a server component
The API client can be used in the same way in a server component, the only difference is that the cookies are not automatically attached to the request, so you need to attach them manually.