Getting Started
Generate your first Dart API client from an OpenAPI spec.
This guide walks through setting up dart_openapi_generator in an existing Flutter or Dart
project. By the end you will have a generated, type-safe API client ready to use. The guide
uses a local OpenAPI spec file — for remote specs see Advanced.
Add dependencies to pubspec.yaml
Add these packages, then run
dart pub get.pubspec.yamldependencies: dart_openapi_generator_annotations: ^0.1.0 dio: ^5.0.0 dev_dependencies: dart_openapi_generator: ^0.1.0 build_runner: ^2.4.0dart_openapi_generator_annotationsis a runtime dependency — it provides the@OpenApiGeneratorannotation your trigger file imports.dart_openapi_generatorandbuild_runnerare dev-only.Place your OpenAPI spec
Copy your OpenAPI 3.x spec (YAML or JSON) into your project, for example at
openapi/api.yaml. The path is relative to yourpubspec.yaml.OpenAPI 3.0, 3.1, and 3.2 are all supported. Swagger 2.x is not supported.Add the annotation
In any Dart file (conventionally
lib/main.dart), import the annotations package and place@OpenApiGeneratoron a class:lib/main.dartimport 'package:dart_openapi_generator_annotations/dart_openapi_generator_annotations.dart'; @OpenApiGenerator( inputSpec: LocalSpec('openapi/api.yaml'), outputDir: 'lib/generated', clientName: 'MyApiClient', ) class $MyApp {}inputSpecandoutputDirare the only required fields. See Annotation Reference for all fields and their defaults.Run code generation
dart run build_runner buildUsedart run build_runner watchto automatically regenerate when your spec changes.After generation,
lib/generated/will contain:api_client.dart— theMyApiClientaggregator classmodels/— one.dartfile per schema, in alphabetical orderservices/— one.dartfile per OpenAPI tag, in alphabetical ordergenerated.dart— a barrel that exports every generated file
Import the barrel
Import the single barrel export so all generated types are available:
lib/main.dartimport 'generated/generated.dart';Instantiate the client
Create your client. If
baseUrlis omitted, the URL fromservers[0]in your spec is used.final client = MyApiClient( baseUrl: 'https://api.example.com/v1', ); // Call any generated service method: final users = await client.users.listUsers();The constructor also accepts
dio(inject a pre-configuredDioinstance),interceptors,connectTimeout, andreceiveTimeout.Add authentication (optional)
If your spec declares
securitySchemes, the generator emits static auth-helper factories on the client class. Pass the factory's return value viainterceptors::final client = MyApiClient( interceptors: [ MyApiClient.bearerAuth('your-bearer-token'), ], );Available factories depend on which security scheme types appear in your spec. See Advanced — Authentication for Bearer, API key, and Basic auth.
What's next?
- Annotation Reference — full documentation of every
@OpenApiGeneratorfield - Generated Code — understand every generated file type
- Advanced — remote specs, auth schemes,
cleanOutput,dateTimeConverter, caching, and OpenAPI 3.2