aws-cdk-lib.aws_apigateway.ApiDefinition

class ApiDefinition

LanguageType name
.NETAmazon.CDK.AWS.APIGateway.ApiDefinition
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsapigateway#ApiDefinition
Javasoftware.amazon.awscdk.services.apigateway.ApiDefinition
Pythonaws_cdk.aws_apigateway.ApiDefinition
TypeScript (source)aws-cdk-lib » aws_apigateway » ApiDefinition

Implemented by AssetApiDefinition, InlineApiDefinition, S3ApiDefinition

Represents an OpenAPI definition asset.

Example

declare const integration: apigateway.Integration;

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

const booksResource = api.root.addResource('books')
booksResource.addMethod('GET', integration);

Initializer

new ApiDefinition()

Methods

NameDescription
bind(scope)Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.
bindAfterCreate(_scope, _restApi)Called after the CFN RestApi resource has been created to allow the Api Definition to bind to it.
static fromAsset(file, options?)Loads the API specification from a local disk asset.
static fromBucket(bucket, key, objectVersion?)Creates an API definition from a specification file in an S3 bucket.
static fromInline(definition)Create an API definition from an inline object.

bind(scope)

public bind(scope: Construct): ApiDefinitionConfig

Parameters

  • scope Construct — The binding scope.

Returns

  • ApiDefinitionConfig

Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.


bindAfterCreate(_scope, _restApi)

public bindAfterCreate(_scope: Construct, _restApi: IRestApi): void

Parameters

  • _scope Construct
  • _restApi IRestApi

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.


static fromAsset(file, options?)

public static fromAsset(file: string, options?: AssetOptions): AssetApiDefinition

Parameters

  • file string
  • options AssetOptions

Returns

  • AssetApiDefinition

Loads the API specification from a local disk asset.


static fromBucket(bucket, key, objectVersion?)

public static fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3ApiDefinition

Parameters

  • bucket IBucket
  • key string
  • objectVersion string

Returns

  • S3ApiDefinition

Creates an API definition from a specification file in an S3 bucket.


static fromInline(definition)

public static fromInline(definition: any): InlineApiDefinition

Parameters

  • definition any

Returns

  • InlineApiDefinition

Create an API definition from an inline object.

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

  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',
        },
      },
    },
  });