OpenAPI to Go Converter - Generate Type-Safe Go Structs from OpenAPI/Swagger Specifications
Free online tool to convert OpenAPI 3.0 and Swagger 2.0 specs to Go structs with proper JSON tags. Validate your spec first with our OpenAPI Validator. Works with oapi-codegen, go-swagger, and the standard net/http package. Uses Go naming conventions with exported fields and proper type mapping per the OAS specification.
How to Convert OpenAPI to Go - Step by Step Guide
Generate type-safe Go structs with JSON tags from OpenAPI 3.0 and Swagger 2.0 specifications
Input Your OpenAPI Specification
Paste your OpenAPI 3.0 or Swagger 2.0 specification in YAML or JSON format. The converter reads all schemas from components/schemas (OAS 3.x) or definitions (Swagger 2.x) and converts each to a Go struct:
Example: OpenAPI 3.0 Spec with Schemas
A spec with two component schemas — Pet and NewPet:
components: schemas: Pet: type: object required: [id, name] properties: id: { type: integer } name: { type: string } species: { type: string } age: { type: integer } NewPet: type: object required: [name] properties: name: { type: string } species: { type: string }
Auto-Generated Go Structs with JSON Tags
Every OpenAPI schema becomes an exported Go struct with proper field types and JSON struct tags:
integer → int64, number → float64, boolean → bool, string → stringrequired array get omitempty in their JSON tag$ref: '#/components/schemas/Pet' become Go type referencesExample: Generated Go Structs
Both schemas converted to Go with correct types and JSON tags:
package api // Pet represents a pet in the store type Pet struct { ID int64 `json:"id"` Name string `json:"name"` Species string `json:"species,omitempty"` Age int64 `json:"age,omitempty"` } // NewPet is used to create a new pet type NewPet struct { Name string `json:"name"` Species string `json:"species,omitempty"` }
Copy and Use in Your Go Project
Copy the generated structs and drop them directly into your Go project:
json.Decoderapi, models).go file and add it directly to your Go project — use json.NewDecoder(resp.Body).Decode(&myStruct) to unmarshal API responsesWhat is OpenAPI to Go Conversion?
OpenAPI to Go conversion takes an OpenAPI 3.0 or Swagger 2.0 API specification and generates native Go structs from the schema definitions. Instead of manually writing Go types for every API response and request body, you let the spec drive the code. This eliminates inconsistencies between your API contract and your Go data models.
Each OpenAPI schema property is mapped to a Go field with the correct type and a encoding/json struct tag matching the original field name. Fields marked as required in the spec get plain JSON tags, while optional fields get omitempty. OpenAPI $ref references are resolved into Go type names, following the Effective Go naming conventions with exported (capitalized) field names.
The generated structs work with the standard net/http package, go-resty, and any other Go HTTP client. For more advanced use cases — including full client generation with methods per endpoint — consider oapi-codegen or go-swagger. Validate your spec first with our OpenAPI Validator to ensure clean output.
Frequently Asked Questions
Which OpenAPI versions are supported?
Both OpenAPI 3.0.x and Swagger 2.0 specs are supported. Both JSON and YAML input formats are accepted. For OAS 3.x, schemas are read from components/schemas. For Swagger 2.0, they are read from definitions.
How are OpenAPI types mapped to Go types?
The mapping follows Go best practices: string → string, integer → int64, number → float64, boolean → bool, array → []Type. OpenAPI $ref references become Go type references. Nested inline objects are extracted as separate named structs.
How are $ref references handled?
Schema references like $ref: '#/components/schemas/User' are resolved to Go type names — in this case User. The referenced schema is also generated as its own struct. This mirrors how tools like oapi-codegen handle schema references in production code generation.
Can I use the structs with the standard net/http package?
Yes. The generated structs use only standard encoding/json tags and work with net/http, go-resty, and any Go HTTP library. Use json.NewDecoder(resp.Body).Decode(&myStruct) to unmarshal API responses directly.
Can I customize the package name?
Yes — use the package name input field to set the Go package declaration. Follow Go naming conventions: lowercase, single word, no underscores. Common choices are api, models, or a specific service name like petstore.
What if I need full client generation with API methods?
For full client generation including HTTP methods per endpoint, use oapi-codegen (OAS 3.x) or go-swagger (Swagger 2.0). This converter focuses on data model structs — a lightweight starting point for projects where you want to write your own HTTP client logic.
Is the OpenAPI to Go converter free?
Yes, completely free with no limitations on spec size or usage. No registration required. Validate your OpenAPI spec first with our OpenAPI Validator, then convert to Go structs — all in your browser.
Related Tools
OpenAPI Validator
Validate OpenAPI 3.0 and Swagger 2.0 specifications online. Check required fields, paths, operations, schemas, and security schemes with instant error reporting.
JSON to OpenAPI
Generate OpenAPI/Swagger specifications from JSON examples. Infer data types and create complete API schemas automatically.
OpenAPI to Rust
Generate type-safe Rust API client code from OpenAPI/Swagger specifications with serde annotations.
OpenAPI to TypeScript
Convert OpenAPI 3.0 and Swagger 2.0 specifications to TypeScript interfaces and types. Auto-generate type-safe TypeScript from any OpenAPI schema.
OpenAPI to Markdown
Convert OpenAPI 3.0 and Swagger 2.0 specifications to readable Markdown API documentation. Generate endpoint tables, parameter docs, and schema references.
Swagger to OpenAPI
Upgrade Swagger 2.0 specifications to OpenAPI 3.0.3 format. Automatically migrates paths, request bodies, security definitions, and components.