Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)41k · #6858

consuming-endpoints-from-client-code

Skill này làm gì

Skill này cho thấy cách gọi một endpoint PostHog từ một ứng dụng riêng, chẳng hạn ứng dụng di động hoặc máy chủ. Nó dành cho lập trình viên muốn dùng dữ liệu PostHog bên trong sản phẩm của chính họ.

Khi nào nên dùng

  • ✓Khi bạn muốn gọi một endpoint PostHog từ ứng dụng hoặc máy chủ của riêng bạn.
  • ✓Khi bạn cần sinh một client có kiểu từ đặc tả OpenAPI của endpoint.
  • ✓Khi một lệnh gọi đến endpoint thất bại do lỗi xác thực hoặc giới hạn tần suất.

Liên kết skill

https://github.com/PostHog/posthog/blob/master/products/endpoints/skills/consuming-endpoints-from-client-code/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/PostHog/posthog/blob/master/products/endpoints/skills/consuming-endpoints-from-client-code/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để gọi endpoint PostHog của tôi từ ứng dụng di động, và yêu cầu trông như thế nào?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill đọc cấu hình của endpoint và đặc tả OpenAPI, rồi cho thấy cách gửi đúng header xác thực và các biến. Skill cũng có thể gợi ý một công cụ sinh mã để tạo client có kiểu.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Consuming endpoints from client code

This skill is the caller-side counterpart to creating-an-endpoint. It helps integrate an existing endpoint into a separate codebase — a mobile app, server backend, customer dashboard, or downstream pipeline. No PostHog code is modified here.

When to use this skill

  • "How do I call my endpoint?" / "What does a request look like?"
  • "Generate a typed TypeScript / Python / Go client for this endpoint"
  • "I'm getting a 401 calling the endpoint" / auth questions
  • "The endpoint rejects my call when I omit user_id" → materialised-endpoint variable questions
  • "How do I handle rate limits?"

If the user is creating the endpoint, use creating-an-endpoint first.

Available tools

Tool Purpose
endpoint-get Full config for a named endpoint, including the query shape and required variables
endpoint-openapi-spec OpenAPI 3.0 spec for one endpoint, ready to feed to a code generator
endpoint-run A live call against the endpoint — useful to confirm a payload works before sharing it with the user's app

The endpoint URL

/api/projects/{team_id}/endpoints/{name}/run
  • team_id is the project ID (numeric). Available in PostHog under project settings, or via projects-get if the user doesn't know it.
  • name is the endpoint name — see endpoints-get-all if the user isn't sure.
  • The trailing /run is required.

POST is the canonical method. GET also works for simple cases without a request body but POST is preferred — variables go in the body.

Auth

Endpoints are authenticated with a personal API key. The header is:

Authorization: Bearer <key>

Keys are scoped — for endpoints, the key needs at least endpoint:read. If the user gets a 403, they're usually missing the scope; if they get a 401, the key is missing or malformed.

Never put a personal API key in client-side code that's shipped to end users (mobile apps, browser JS). Personal API keys grant scoped account access. For customer-facing apps, route through the user's own backend, which holds the key.

The request payload

{
  "variables": { "code_name_1": value, "code_name_2": value },
  "limit": 100,
  "offset": 0,
  "refresh": "cache"
}
Field Notes
variables Keyed by code_name for HogQL endpoints; for insight endpoints with breakdowns, key is the breakdown property name
limit Max rows returned.
offset Skip rows. Only HogQL endpoints
refresh "cache" (return cached results if fresh enough), "force" (always recalculate), "direct" (bypass materialisation, materialised endpoints only). Default is "cache"

Call endpoint-get to see the exact variable shape. The response includes the query definition with declared variables — each variable's code_name is what the client should send.

Materialised endpoints: all variables are required

If endpoint-get shows is_materialized: true on the current version, the endpoint requires every declared variable to be passed on each call. This is a security boundary — without filters, a single call would return the entire pre-aggregated dataset.

Common symptom: the user's app worked when the endpoint was unmaterialised, then started returning 400 errors after materialisation was enabled. The error message lists which variables are missing.

Optional/partial variables on materialised endpoints are a known limit

Câu hỏi thường gặp

Skill này có thay đổi gì trong PostHog không?

Không. Nó chỉ giúp bạn kết nối với một endpoint từ một mã nguồn khác.

Nó có thể sinh client bằng mọi ngôn ngữ không?

Nó bao gồm việc sinh client có kiểu cho TypeScript, Python và Go, dùng các công cụ sinh như openapi-generator hoặc @hey-api/openapi-ts.

Nếu lệnh gọi bị từ chối vì thiếu biến thì sao?

Skill giải thích các biến phải có dạng nào đối với endpoint materialized, để các giá trị cần thiết được đưa vào.

Chi tiết

Mô tả gốc của tác giả

Wire a PostHog endpoint into a client app or SDK. Covers fetching the OpenAPI spec, generating a typed client with openapi-generator or @hey-api/openapi-ts, sending the right auth header, shaping the variables payload (HogQL code_name vs insight breakdown property), handling rate-limit and materialised-endpoint error responses. Use when the user says "how do I call my endpoint", "generate a client for this", or "what auth header do I use".

repo: PostHog/posthog

Độ phổ biến

Sao GitHub
41k
Vị trí xếp hạng
#6858
Giấy phép
—
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự