adopting-generated-api-types
Skill727 starsChanged yesterdayMITView on GitHub
Use when migrating frontend code from manual API client calls (`api.get`, `api.create`, `api.surveys.get`, `api.dashboards.list`, `new ApiRequest()`) and handwritten TypeScript interfaces to generated API functions and types. Triggers on files importing from `lib/api`, files with `api.get<`, `api.create<`, `api.<entity>.<method>`, manual interface definitions that duplicate backend serializers, or any frontend file that constructs API URLs by hand. Covers the full replacement workflow — finding the generated equivalent, swapping imports, adapting call sites, and removing dead manual types.
We split this skill into the rules it gives an agent
13 rules. About 0 are common, 6 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 63
All three patterns should be replaced with generated functions where available.
- Distinctive: line 77
Look at the existing call and extract:
- Very specific: line 103
Search by the get*Url helper functions — every generated function has a URL builder above it
- Very specific: line 104
Search api.schemas.ts for the type name with Api suffix
- Very specific: line 106
If no generated function exists, the backend endpoint may lack @extend_schema or @validated_request.
- Distinctive: line 129
Create/update with request bodies
- Distinctive: line 136
Update downstream references from the handwritten type to the generated one:
- Distinctive: line 148
After migrating all usages of a handwritten type:
- Very specific: line 150
Remove the type definition from ~/types or the local file
- Very specific: line 152
Run pnpm --filter=@posthog/frontend typescript:check to verify no breakage
- Very specific: line 195
Use import type for types to enable proper tree-shaking.
- Distinctive: line 212
Grep for leftover manual types: search for the old type name across the codebase
- Very specific: line 217
Backend side: use improving-drf-endpoints to fix serializers that produce poor types
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Adopting generated API types
Overview
PostHog generates TypeScript API client functions and types from Django serializers via the OpenAPI pipeline:
Django serializer → drf-spectacular → OpenAPI JSON → Orval → TypeScript (api.ts + api.schemas.ts + api.zod.ts)Generated files live in:
- Core:
frontend/src/generated/core/api.ts,api.schemas.ts, andapi.zod.ts - Products:
products/<product>/frontend/generated/api.ts,api.schemas.ts, andapi.zod.ts
Generated types use the Api suffix (DashboardApi, SurveyApi). Handwritten types never do.
This skill guides replacing manual API calls and handwritten types with generated equivalents.
The three manual patterns to migrate
The legacy frontend/src/lib/api.ts (~6000 lines) has three layers, all migration targets:
1. High-level object API (most common)
Domain-specific convenience methods on the api object:
api.surveys.get(id)
api.surveys.create(data)
api.dashboards.list()
api.cohorts.update(id, data)
api.actions.create(data)These are the most widely used pattern — every entity has its own namespace with CRUD plus custom methods (e.g., api.surveys.getResponsesCount(), api.dashboards.streamTiles()).
2. Raw HTTP methods with manual URLs
api.get<SomeType>(`api/projects/${id}/surveys/`)
api.create<SomeType>(`api/projects/${id}/surveys/`, data)
api.update<SomeType>(url, data)
api.put<SomeType>(url, data)
api.delete(url)3. ApiRequest builder (fluent URL construction)
const url = new ApiRequest().surveys().assembleFullUrl()
const response = await api.get(url)
// or directly:
await new ApiRequest().survey(surveyId).withAction('summarize_responses').create({ data })All three patterns should be replaced with generated functions where available.
When to use
- Touching a file that calls
api.<entity>.<method>()(e.g.,api.surveys.get()) - Touching a file that calls
api.get<T>(...),api.create<T>(...), etc. - Touching a file that uses
new ApiRequest()to build URLs - Touching a file that imports handwritten interfaces from
~/typesfor API response shapes - Cleaning up frontend code after backend serializer improvements
Step-by-step workflow
1. Identify what the manual call does
Look at the existing call and extract:
- HTTP method — GET, POST, PUT, PATCH, DELETE
- Entity and action — what resource, what operation
- Type parameter — the handwritten type used for the response
2. Find the generated equivalent
Generated function names follow the {resource}{Action} convention:
surveysList — GET /api/projects/{id}/surveys/
surveysCreate — POST /api/projects/{id}/surveys/
surveysRetrieve — GET /api/projects/{id}/surveys/{id}/
surveysPartialUpdate — PATCH /api/projects/{id}/surveys/{id}/
surveysDestroy — DELETE /api/projects/{id}/surveys/{id}/Where to search:
- Core endpoints:
frontend/src/generated/core/api.ts - Product endpoints:
products/<product>/frontend/generated/api.ts
Search strategies:
- Grep for the entity name in the generated
api.tsfiles - Search by the
get*Urlhelper functions — every generated function has a URL builder above it - Search
api.schemas.tsfor the type name withApisuffix
If no generated function exists, the backend endpoint may lack @extend_schema or @validated_request. Fix the backend first using the improving-drf-endpoints skill, then run hogli build:openapi.
Custom actions (like api.surveys.summarize_responses()) may not have generated equivalents if the backend @action lacks @extend_schema. Check generated files first; if missing, fix the backend.
3. Check type compatibility
Compare the handwritten type with the generated Api type. Key differences:
readonlymodifiers — generated types mark read-only fields- Optional vs required — generated types reflect
required=precisely - Nullability —
nulltypes are explicit - Extra fields — generated types may include fields the handwritten type omits
See type-compatibility.md for details.
4. Replace the call
See migration-patterns.md for detailed before/after examples covering:
- High-level object API (
api.surveys.get()→surveysRetrieve()) - Raw HTTP methods (
api.get<T>(url)→ generated function) - ApiRequest builder → generated function
- Paginated list calls
- Create/update with request bodies
- Delete calls
- Kea logic loaders and listeners
- Calls with abort signals
5. Replace the type at usage sites
Update downstream references from the handwritten type to the generated one:
// Before
function renderSurvey(survey: Survey): JSX.Element { ... }
// After
function renderSurvey(survey: SurveyApi): JSX.Element { ... }6. Clean up dead types
After migrating all usages of a handwritten type:
- Remove the type definition from
~/typesor the local file - Remove unused imports
- Run
pnpm --filter=@posthog/frontend typescript:checkto verify no breakage
Decision guide
| Scenario | Action |
| Generated function exists | Replace manual call with generated function |
| Generated type exists but function doesn't | Use the generated type as the generic parameter on the manual call, file a follow-up to add @extend_schema |
| Neither exists | Keep the manual pattern, fix the backend serializer/viewset first |
| Custom action without generated equivalent | Keep the api.<entity>.<method>() call, fix the backend @action annotation first |
| Generated type has different shape than handwritten | Adapt call sites to the generated shape — the serializer is the source of truth |
| Code mutates the response object | Use a local mutable copy: const mutable = { ...response } and mutate that |
| Need both read and write types | Use FooApi for reads, derive write types via Parameters<typeof fooCreate>[1] or use PatchedFooApi |
Import conventions
// Core generated functions — import from api.ts
import { domainsList, domainsCreate, domainsRetrieve } from '~/generated/core/api'
// Core generated types — import type from api.schemas.ts
import type { OrganizationDomainApi } from '~/generated/core/api.schemas'
// Core generated Zod schemas — import from api.zod.ts
import { DomainsCreateBody } from '~/generated/core/api.zod'
// Product generated functions — NO tilde prefix, use 'products/' path
import { surveysList, surveysRetrieve } from 'products/surveys/frontend/generated/api'
import type { SurveyApi } from 'products/surveys/frontend/generated/api.schemas'
import { SurveysCreateBody } from 'products/surveys/frontend/generated/api.zod'
// Within a product, relative imports also work
import { logsAlertsCreate } from '../generated/api'
import type { LogsAlertConfigurationApi } from '../generated/api.schemas'
import { LogsAlertsCreateBody } from '../generated/api.zod'Path rules:
- Core:
~/generated/core/...(tilde prefix) - Products from outside:
products/<product>/frontend/generated/...(no tilde) - Products from inside: relative
../generated/...or./generated/...
Use import type for types to enable proper tree-shaking.
How generated functions work under the hood
Generated functions wrap the same api module via api-orval-mutator.ts:
surveysList(projectId, params)
→ apiMutator(url, { method: 'GET' })
→ api.get(url)Switching to generated functions does not change HTTP behavior — same cookies, same CSRF, same error handling. The only difference is type safety and URL construction.
Verifying the migration
- TypeScript check:
pnpm --filter=@posthog/frontend typescript:check - Grep for leftover manual types: search for the old type name across the codebase
- Run relevant tests:
hogli test <test_file>
Related
- Backend side: use
improving-drf-endpointsto fix serializers that produce poor types - Type system docs:
docs/published/handbook/engineering/type-system.md - API mutator:
frontend/src/lib/api-orval-mutator.ts - Regenerate types:
hogli build:openapi
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository124 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- hog/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adding-today-highlightsSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-llm-gateway-paritySkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- building-product-empty-statesSkill
- building-the-toolbarSkill
- clickhouse-migrationsSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- django-migrationsSkill
- django-startup-timeSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- finding-llm-gateway-migration-candidatesSkill
- fixing-flaky-testsSkill
- formatting-slack-messagesSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-toolsSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- ingestion-pipeline-doctor-nodejsSkill
- instrumenting-first-party-metricsSkill
- The first 60 shown.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

