Code¶
1 endpoint for generating TypeScript type definitions from JSON.
| Method | Endpoint | Purpose |
|---|---|---|
POST |
/v1/convert/json-to-typescript |
Generate TypeScript interfaces from a JSON object |
Python SDK Examples¶
The JSON-to-TypeScript endpoint is not yet exposed as a named method on the Convert SDK class. Call it directly via the underlying HTTP client.
Simple object to TypeScript¶
import httpx
data = {
"userId": 1,
"username": "janedoe",
"email": "[email protected]",
"isActive": True,
}
response = httpx.post(
"https://convert.toolkitapi.io/v1/convert/json-to-typescript",
json={"data": data, "root_name": "User"},
headers={"X-API-Key": "tk_..."},
)
result = response.json()
print(result["typescript"])
# interface User {
# userId: number;
# username: string;
# email: string;
# isActive: boolean;
# }
print(f"Generated {result['interface_count']} interface(s)")
Nested object with sub-interfaces¶
Nested objects are automatically extracted into their own named interfaces using PascalCase.
import httpx
data = {
"orderId": "ORD-9921",
"customer": {
"name": "Alice Green",
"address": {
"street": "123 Main St",
"city": "Springfield",
"zipCode": "12345",
},
},
"total": 149.99,
"isPaid": False,
}
response = httpx.post(
"https://convert.toolkitapi.io/v1/convert/json-to-typescript",
json={"data": data, "root_name": "Order"},
headers={"X-API-Key": "tk_..."},
)
print(response.json()["typescript"])
# interface Address {
# street: string;
# city: string;
# zipCode: string;
# }
#
# interface Customer {
# name: string;
# address: Address;
# }
#
# interface Order {
# orderId: string;
# customer: Customer;
# total: number;
# isPaid: boolean;
# }
Array of typed objects¶
Arrays of objects are typed as T[] with a generated child interface.
import httpx
data = {
"page": 1,
"items": [
{"id": 1, "title": "Widget", "price": 9.99},
{"id": 2, "title": "Gadget", "price": 14.99},
],
}
response = httpx.post(
"https://convert.toolkitapi.io/v1/convert/json-to-typescript",
json={"data": data, "root_name": "ProductList"},
headers={"X-API-Key": "tk_..."},
)
print(response.json()["typescript"])
# interface ItemsItem {
# id: number;
# title: string;
# price: number;
# }
#
# interface ProductList {
# page: number;
# items: ItemsItem[];
# }
Write interfaces to a file¶
import httpx
from pathlib import Path
data = {
"token": "abc123",
"expiresAt": "2025-12-31T00:00:00Z",
"user": {"id": 42, "role": "admin"},
}
response = httpx.post(
"https://convert.toolkitapi.io/v1/convert/json-to-typescript",
json={"data": data, "root_name": "AuthResponse"},
headers={"X-API-Key": "tk_..."},
)
result = response.json()
Path("auth-types.ts").write_text(result["typescript"])
print(f"Wrote {result['interface_count']} interface(s) to auth-types.ts")
Request Parameters¶
| Parameter | Type | Description |
|---|---|---|
data |
object | JSON object to convert |
root_name |
string | Name for the root interface (default Root, max 100 characters) |
Response Fields¶
| Field | Type | Description |
|---|---|---|
typescript |
string | Complete TypeScript interface declarations as a string |
interface_count |
integer | Number of interfaces generated |
Tip
Interface names are derived using PascalCase. Nested objects become their own named interfaces and are referenced by name — the deeper the nesting, the more interfaces are generated. Use root_name to set a meaningful entry-point type name.