Save a real response and infer the types from it with a generator: quicktype on the command line or a browser tool. If the API publishes an OpenAPI or JSON Schema description, generate the types from that instead, because it says which fields are optional and which values are allowed, and a sample cannot.
Pick a response with as many fields filled in as you can find. Several samples are better than one: jq -s . user1.json user2.json > users.json combines them into an array, and generators type an array's items together, so a key missing from some of them can be marked optional.
With quicktype
quicktype infers types for many languages from JSON samples:
npx quicktype user.json -o user.ts --just-types --top-level User
--just-types emits plain interfaces; without it, quicktype also writes functions that convert and check the data at runtime. The output language follows the file extension of -o, or set it with --lang ts.
From an OpenAPI description
If the API has an OpenAPI (Swagger) file, openapi-typescript generates types for every endpoint:
These come from the API's published contract rather than one response, so required and optional fields, enums and nullable values match what the API promises, as long as the description is kept up to date.
What to watch for
Optional and nullable fields: a field absent from your sample is missing from the type, and one that happened to be null is typed as null. Check the API documentation and add ? or | null where needed.
Narrow values: a status of "active" is typed as string. If the API documents a fixed set, write a union such as "active" | "suspended".
Dates and large numbers: JSON has no date type, so timestamps arrive as strings or numbers. Integers above 2^53 lose precision in JSON.parse, which is why many APIs send large IDs as strings.
Empty arrays: [] gives no element type, so a generator falls back to any[] or similar. Fill it in once you know what the array holds.
Types are not checks: TypeScript types disappear when the code is compiled, and await response.json() returns whatever the server sent. If the data must match, validate it, for example with a Zod schema.
Do it in your browser
JSON to TypeScript infers interfaces from JSON you paste or load from a file, updating as you type. Each nested object gets its own named interface, an array of objects is typed from every item with missing keys marked optional, and a field that is null in some items becomes T | null. You choose the root interface name. Interfaces are not exported, and an empty array becomes any[]. Nothing leaves your browser, which matters when the sample holds customer data. For runtime validation, JSON to Zod Schema builds a schema from the same sample.
Frequently asked questions
How do I convert JSON to a TypeScript interface?
Paste a sample into a generator such as quicktype or the XUtil JSON to TypeScript tool, which infers a type for each key. Then check fields that can be missing or null, since one sample cannot show them.
Should I use interface or type for API responses?
Either works for object shapes. Interfaces can be extended and merged across declarations, while type aliases can also name unions and tuples; pick one and stay consistent.
Do TypeScript types validate JSON at runtime?
No. Types are removed when the code is compiled, so a response that does not match is not caught. Use a validator such as Zod, or JSON Schema with Ajv, when the data must be checked.
How do I get types for a whole API?
If it publishes an OpenAPI description, generate types from it with openapi-typescript. That covers every endpoint and records which fields are optional, which a sample response cannot.