# ApiDefinition

Represents an OpenAPI definition asset.

Superclass of: [AssetApiDefinition](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.AssetApiDefinition.html), [InlineApiDefinition](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.InlineApiDefinition.html), [S3ApiDefinition](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.S3ApiDefinition.html)

- Source: [`aws-apigateway/lib/api-definition.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-apigateway/lib/api-definition.ts#L13)
- Type last changed: v2.224.0 · 13 Nov 2025

## Example

TypeScript:

```ts
const api = new apigateway.SpecRestApi(this, 'books-api', {
  apiDefinition: apigateway.ApiDefinition.fromAsset('path-to-file.json'),
  mode: apigateway.RestApiMode.MERGE
});
```

Python:

```python
api = apigateway.SpecRestApi(self, "books-api",
    api_definition=apigateway.ApiDefinition.from_asset("path-to-file.json"),
    mode=apigateway.RestApiMode.MERGE
)
```

Java:

```java
SpecRestApi api = SpecRestApi.Builder.create(this, "books-api")
        .apiDefinition(ApiDefinition.fromAsset("path-to-file.json"))
        .mode(RestApiMode.MERGE)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var api = new SpecRestApi(this, "books-api", new SpecRestApiProps {
    ApiDefinition = ApiDefinition.FromAsset("path-to-file.json"),
    Mode = RestApiMode.MERGE
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
api := apigateway.NewSpecRestApi(this, jsii.String("books-api"), &SpecRestApiProps{
	ApiDefinition: apigateway.ApiDefinition_FromAsset(jsii.String("path-to-file.json")),
	Mode: apigateway.RestApiMode_MERGE,
})
```

## Initializer

TypeScript:

```ts
new ApiDefinition()
```

Python:

```python
ApiDefinition()
```

Java:

```java
new ApiDefinition()
```

C# / .NET:

```csharp
new ApiDefinition()
```

Golang:

```go
NewApiDefinition()
```

## Methods

- `bind(scope: Construct): ApiDefinitionConfig` — Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.

  **Parameters**

  - `scope` — The binding scope.

- `bindAfterCreate(_scope: Construct, _restApi: IRestApiRef): void` — Called after the CFN RestApi resource has been created to allow the Api Definition to bind to it.

  Specifically it's required to allow assets to add
  metadata for tooling like SAM CLI to be able to find their origins.

- `fromAsset(file: string, options?: AssetOptions): AssetApiDefinition` (static) — Loads the API specification from a local disk asset.

- `fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3ApiDefinition` (static) — Creates an API definition from a specification file in an S3 bucket.

- `fromInline(definition: any): InlineApiDefinition` (static) — Create an API definition from an inline object.

  The inline object must follow the
  schema of OpenAPI 2.0 or OpenAPI 3.0

  ```ts
    apigateway.ApiDefinition.fromInline({
      openapi: '3.0.2',
      paths: {
        '/pets': {
          get: {
            'responses': {
              200: {
                content: {
                  'application/json': {
                    schema: {
                      $ref: '#/components/schemas/Empty',
                    },
                  },
                },
              },
            },
            'x-amazon-apigateway-integration': {
              responses: {
                default: {
                  statusCode: '200',
                },
              },
              requestTemplates: {
                'application/json': '{"statusCode": 200}',
              },
              passthroughBehavior: 'when_no_match',
              type: 'mock',
            },
          },
        },
      },
      components: {
        schemas: {
          Empty: {
            title: 'Empty Schema',
            type: 'object',
          },
        },
      },
    });
  ```
