MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / posthogQuery

Function posthogQuery

apps/web/src/lib/posthog-query.ts:26–60  ·  view source on GitHub ↗
(name: string, query: string)

Source from the content-addressed store, hash-verified

24 * @returns Query response with results or error
25 */
26export async function posthogQuery(name: string, query: string): Promise<PostHogQueryResponse> {
27 const apiKey = getEnvVariable('POSTHOG_QUERY_API_KEY');
28 if (!apiKey) {
29 throw new Error('No PostHog Query API Key');
30 }
31
32 const response = await fetch('https://us.posthog.com/api/projects/141915/query/', {
33 method: 'POST',
34 headers: {
35 Authorization: `Bearer ${apiKey}`,
36 'Content-Type': 'application/json',
37 },
38 body: JSON.stringify({
39 query: {
40 kind: 'HogQLQuery',
41 query,
42 },
43 name,
44 }),
45 cache: 'no-store',
46 });
47
48 if (!response.ok) {
49 return {
50 status: 'error',
51 statusCode: response.status,
52 error: await response.json().catch(() => ({ error: 'Unknown error' })),
53 };
54 }
55
56 return {
57 status: 'ok',
58 body: await response.json(),
59 };
60}
61
62const CACHE_TTL_SECONDS = 60 * 60 * 24; // 24 hours
63const MEMORY_CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour

Calls 3

getEnvVariableFunction · 0.90
jsonMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected