# JSON Schema Converter API

> Convert JSON Schema to TypeScript, Zod or Pydantic, upgrade draft-04/06/07 to 2020-12, convert to and from OpenAPI 3.0, and bundle or dereference $ref.

- Endpoint: `POST | GET https://apibox.io.vn/v1/schema-convert`
- Category: Data & JSON
- HTML docs: https://apibox.io.vn/docs/schema-convert/ (Vietnamese: https://apibox.io.vn/vi/docs/schema-convert/)
- Online tool: https://apibox.io.vn/tools/schema-convert/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

Send a JSON Schema (object, JSON or YAML text, file or URL) and pick `to`. Code: `typescript` (interfaces / types with JSDoc), `zod` (schemas with min / max / regex / email / uuid / datetime refinements, `.strict()`, `z.lazy` for recursion, plus `z.infer` types) and `pydantic` (Pydantic v2 models with `Field()` constraints, aliases for non-identifier keys, `extra="forbid"`); definitions / $defs become named types.

Drafts: `2020-12`, `2019-09`, `draft-07`, `draft-06`, `draft-04` upgrade or downgrade the schema (definitions ↔ $defs, id ↔ $id, boolean ↔ numeric exclusiveMinimum / Maximum, items array ↔ prefixItems, dependencies ↔ dependentRequired / dependentSchemas, const ↔ enum, keywords next to $ref). OpenAPI: `openapi-3.0` turns JSON Schema into an OpenAPI 3.0 Schema Object (nullable, example, exclusive bounds, $defs → components.schemas); `json-schema` does the reverse and accepts a whole OpenAPI 3.0 (or Swagger 2.0) document. References: `bundle` pulls external $refs (from `refs` = {"common.json": {...}} or URLs with `fetch=1`) into $defs; `dereference` inlines every $ref and reports cycles (kept as $ref, or `circular=error`).

Whatever cannot be expressed in the target is listed in `warnings`. Related: /v1/json-to-types infers types from sample JSON, /v1/schema-mock makes example data, /v1/openapi-tools works on whole API documents. `format=text` returns only the code.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `schema` | body | object | yes |  | JSON Schema (object, or JSON / YAML text; max 3 MB). Or a file / `url`. |
| `to` | body|query | string | no | `typescript` | `typescript`, `zod`, `pydantic`, `2020-12`, `2019-09`, `draft-07`, `draft-06`, `draft-04`, `openapi-3.0`, `json-schema`, `bundle` or `dereference`. |
| `from` | body|query | string | no |  | Source dialect if `$schema` is missing: `draft-04` … `2020-12` or `openapi-3.0` (auto-detected). |
| `name` | body|query | string | no |  | Name of the root type (default: title or `Root`). |
| `refs` | body | object | no |  | External documents for $ref, keyed by URI or file name. |
| `fetch` | body|query | boolean | no |  | `1`: download http(s) $ref targets (max 20). |
| `circular` | body|query | string | no | `ref` | dereference: `ref` (keep cycles as $ref) or `error`. |
| `format` | query | string | no |  | `text`: return only the generated code as text/plain. |

## Example request

```bash
curl -X POST "https://apibox.io.vn/v1/schema-convert" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"schema":{"title":"Order","type":"object","required":["id","items"],"properties":{"id":{"type":"string","format":"uuid"},"items":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/Item"}},"note":{"type":["string","null"],"maxLength":500}},"$defs":{"Item":{"type":"object","required":["sku","qty"],"properties":{"sku":{"type":"string"},"qty":{"type":"integer","minimum":1}}}}},"to":"zod"}'
```

## Example response

```json
{
  "to": "zod",
  "language": "zod",
  "code": "// Generated from JSON Schema by apibox.io.vn /v1/schema-convert\nimport { z } from 'zod';\n\nexport const ItemSchema = z.object({\n  sku: z.string(),\n  qty: z.number().int().gte(1),\n});\nexport type Item = z.infer<typeof ItemSchema>;\n\nexport const OrderSchema = z.object({\n  id: z.string().uuid(),\n  items: z.array(ItemSchema).min(1),\n  note: z.string().max(500).nullable().optional(),\n});\nexport type Order = z.infer<typeof OrderSchema>;\n",
  "types": [
    "Order",
    "Item"
  ],
  "warnings": []
}
```

## Use cases

- Generate TypeScript types and Zod validators from your API's JSON Schemas
- Move old draft-04 / draft-07 schemas to 2020-12 for modern validators
- Reuse JSON Schemas inside an OpenAPI 3.0 spec (and back)
- Build Pydantic models or self-contained schemas for LLM tool calling

## FAQ

### Which Zod version is generated?

The Zod 3 API (z.object, z.string().email()…), which Zod 4 still accepts.

### Can it convert TypeScript to JSON Schema?

No, that needs the TypeScript compiler. Use JSON Schema as the source, or infer from sample data with json-to-types.

### What happens with external $ref?

Pass the referenced documents in refs, or set fetch=1 to download public http(s) URLs (up to 20 documents).
