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

Step 1

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:

Paste directly: Copy your spec from Swagger Editor, your API gateway config, or spec files in your project
Upload a file: Click "Upload" to load an openapi.json, swagger.yaml, or .yml file from disk
Try the sample: Click "Sample" to load a Pet Store spec and see the Go struct output immediately

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 }
Step 2

Auto-Generated Go Structs with JSON Tags

Every OpenAPI schema becomes an exported Go struct with proper field types and JSON struct tags:

Type mapping: OpenAPI integerint64, numberfloat64, booleanbool, stringstring
Optional fields: Fields not in the required array get omitempty in their JSON tag
$ref resolution: Schema references like $ref: '#/components/schemas/Pet' become Go type references

Example: 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"`
}
Step 3

Copy and Use in Your Go Project

Copy the generated structs and drop them directly into your Go project:

Use with net/http: Decode API responses directly into the generated structs using json.Decoder
Customize package name: Type your Go package name in the field above the output editor — follow Go naming conventions: lowercase, single word, no underscores (e.g. api, models)
Download as .go file: Save the output as a .go file and add it directly to your Go project — use json.NewDecoder(resp.Body).Decode(&myStruct) to unmarshal API responses

What 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: stringstring, integerint64, numberfloat64, booleanbool, 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.