Documentation
API

Use API in frontend

How to use the endpoint in your application depends on whether you want to call the endpoint on the server or on the client.

Server components

To call the endpoint from a server component you need to create an api caller and call the endpoint like so:

import { createApiCaller } from 'api';
 
function MyServerComponent() {
  const apiCaller = await createApiCaller();
  const plans = await apiCaller.billing.plans();
 
  // do something with the data...
  return <div>{JSON.stringify(plans)}/div>;
}

Client components

To call the endpoint in the frontend you can import the apiClient and use the endpoint like so:

import { apiClient } from "@shared/lib";
 
export function MyClientComponent() {
  const { data: publishedPosts, isLoading: loadingPosts } =
    apiClient.posts.publishedPosts.useQuery();
 
  if (loadingPosts) {
    return <div>Loading...</div>;
  }
 
  return (
    <div>{data?.map((post) => <div key={post.id}>{post.title}</div>)}</div>
  );
}

On this page