Skip to content
v2.278.0

ApiDefinition Class ​

Represents an OpenAPI definition asset.

Example ​

ts
const api = new apigateway.SpecRestApi(this, 'books-api', {
  apiDefinition: apigateway.ApiDefinition.fromAsset('path-to-file.json'),
  mode: apigateway.RestApiMode.MERGE
});
python
api = apigateway.SpecRestApi(self, "books-api",
    api_definition=apigateway.ApiDefinition.from_asset("path-to-file.json"),
    mode=apigateway.RestApiMode.MERGE
)
java
SpecRestApi api = SpecRestApi.Builder.create(this, "books-api")
        .apiDefinition(ApiDefinition.fromAsset("path-to-file.json"))
        .mode(RestApiMode.MERGE)
        .build();
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
});
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 ​

ts
new ApiDefinition()
python
ApiDefinition()
java
new ApiDefinition()
csharp
new ApiDefinition()
go
NewApiDefinition()

Methods ​

bind(scope: Construct): ApiDefinitionConfigdef bind(self, scope: Construct) -> ApiDefinitionConfigpublic ApiDefinitionConfig bind(Construct scope)public ApiDefinitionConfig Bind(Construct scope)func Bind(Scope Construct) ApiDefinitionConfig #
Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.
1 parameter

Parameters

  • scopescopescopescopeScope — The binding scope.
bindAfterCreate(_scope: Construct, _restApi: IRestApiRef): voiddef bind_after_create(self, _scope: Construct, _rest_api: IRestApiRef) -> Nonepublic void bindAfterCreate(Construct _scope, IRestApiRef _restApi)public void BindAfterCreate(Construct _scope, IRestApiRef _restApi)func BindAfterCreate(_scope Construct, _restApi IRestApiRef) #
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@staticmethod def from_asset(file: str, options: typing.Optional[AssetOptions] = None) -> AssetApiDefinitionpublic static AssetApiDefinition fromAsset(String file, AssetOptions options)public static AssetApiDefinition FromAsset(string file, AssetOptions? options = null)func FromAsset(File *string, Options AssetOptions) AssetApiDefinition static #
Loads the API specification from a local disk asset.
fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3ApiDefinition@staticmethod def from_bucket(bucket: IBucket, key: str, object_version: typing.Optional[str] = None) -> S3ApiDefinitionpublic static S3ApiDefinition fromBucket(IBucket bucket, String key, String objectVersion)public static S3ApiDefinition FromBucket(IBucket bucket, string key, string? objectVersion = null)func 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@staticmethod def from_inline(definition: Any) -> InlineApiDefinitionpublic static InlineApiDefinition fromInline(Object definition)public static InlineApiDefinition FromInline(object definition)func FromInline(Definition interface{}) 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

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