@vizertech/nextjs-capi
Version:
NextJS 13 Wrapper for fb pixel and capi for graph v17.0 API
31 lines (26 loc) • 743 B
text/typescript
type ApiGraph = {
endpoint: string,
body: any
}
import debug from '../lib/debug';
async function capiAPI<T>({
endpoint = '',
body = null
}: ApiGraph): Promise<T> {
const pixelId = process.env.NEXT_PUBLIC_PIXEL_ID ?? '';
try {
const raw = await fetch(`https://graph.facebook.com/${process.env.NEXT_PUBLIC_GRAPH_VERSION}/${pixelId}/${endpoint}?access_token=${process.env.FB_ACCESS_TOKEN}`, {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(body)
});
let res = await raw.json();
return res as Promise<T>;
}
catch (error) {
throw error;
}
}
export default capiAPI;