Generate types from a real API response
Paste the JSON you get back from an API and this tool writes the matching types for you. Pick TypeScript interfaces for type checking, a Zod schema when you also want to validate responses at runtime, or Java classes with Jackson annotations for a Spring Boot client.
How arrays are handled
Real responses are rarely uniform. When an array contains several objects, the tool merges all of them instead of looking only at the first item:
- A key that appears in some items but not others becomes optional (
coupon?: string). - A key that is
nullin some items becomes nullable (total: number | null). - Whole numbers and decimals in the same field become
number(orDoublein Java). - An empty array becomes
unknown[]because there is nothing to infer from. Paste a sample with data to get a real type.
The more varied your sample, the more accurate the result. If an endpoint is paginated, paste a page with several records rather than a single one.
Using the Zod output
Validate a response before you trust it:
const res = await fetch("/api/users/1042");
const user = UserSchema.parse(await res.json()); // throws if the shape changed
// user is fully typed as User Use safeParse instead of parse if you want to handle a mismatch without an exception. Turn on .strict() if unexpected extra keys should count as errors.
Using the Java output
Records need Java 16 or newer and Jackson 2.12 or newer, which every current Spring Boot version includes. Keys that are not valid Java names, like created-at, get an @JsonProperty annotation so they still map correctly. Integers become Long because JSON numbers can exceed the range of int, and every type is boxed so a missing or null value doesn’t throw.
User user = restClient.get()
.uri("/api/users/{id}", 1042)
.retrieve()
.body(User.class);@JsonIgnoreProperties(ignoreUnknown = true) is added to every class, so new fields an API adds later won’t break deserialization.
Limitations
Types are inferred from the sample, not from an API specification. Dates stay as strings, and a field that is always null in your sample can’t be typed more precisely than null. If the API publishes an OpenAPI spec, treat that as the source of truth and use this tool for quick work and undocumented endpoints.