Small React example
We recommend using any library that you like, React Query, Axios, Fetch, etc. In this example, we will use fetcher with React Query.
subscription-provider.js
const CustomerContext = createContext({});
export default function SubscriptionProvider({children}) {
const { data: session } = useSession()
const { data: customer,} = useQuery({
queryKey: ['fetchCustomer'],
queryFn: async () => await getCustomer(session.email), // your session object should have the email
enabled: true,
staleTime: 10, // 10 min
})
return (
<CustomerContext.Provider value={{ customer }}>
{children}
</CustomerContext.Provider>
)
}
export function useCustomer() {
const context = useContext(CustomerContext);
if (context === undefined) {
throw new Error("Context must be used within a Provider");
}
return context;
}
customer-service.js
const CUSTOMER_URL = `https://subscriptionplan.dev/api/v1/project/${PROJECT_ID}/customers`
export const getCustomer = async (email) => {
const response = await fetch(`${CUSTOMER_URL}/${email}`)
return await response.json()
}
💡
Remember to replace the {projectId}
with your public project id and {email}
with customer email.
You are ready to go!