# Model

Extends: Construct › Resource › Model
Implements: IResource, IModel, IEnvironmentAware, IConstruct, IDependable

- CloudFormation: [`AWS::ApiGateway::Model`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-apigateway-model.html)
- L1 type: [`CfnModel`](/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.CfnModel.html)
- Source: [`aws-apigateway/lib/model.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-apigateway/lib/model.ts#L115)
- Type last changed: v2.196.0 · 16 May 2025

## Example

```ts
declare const api: apigateway.RestApi;

// We define the JSON Schema for the transformed valid response
const responseModel = api.addModel('ResponseModel', {
  contentType: 'application/json',
  modelName: 'ResponseModel',
  schema: {
    schema: apigateway.JsonSchemaVersion.DRAFT4,
    title: 'pollResponse',
    type: apigateway.JsonSchemaType.OBJECT,
    properties: {
      state: { type: apigateway.JsonSchemaType.STRING },
      greeting: { type: apigateway.JsonSchemaType.STRING }
    }
  }
});

// We define the JSON Schema for the transformed error response
const errorResponseModel = api.addModel('ErrorResponseModel', {
  contentType: 'application/json',
  modelName: 'ErrorResponseModel',
  schema: {
    schema: apigateway.JsonSchemaVersion.DRAFT4,
    title: 'errorResponse',
    type: apigateway.JsonSchemaType.OBJECT,
    properties: {
      state: { type: apigateway.JsonSchemaType.STRING },
      message: { type: apigateway.JsonSchemaType.STRING }
    }
  }
});
```

## Initializer

```ts
new Model(scope: Construct, id: string, props: ModelProps)
```

## Construct Props

- `contentType`: string = 'application/json' — The content type for the model.

  You can also force a
  content type in the request or response model mapping.

- `description`: string = None — A description that identifies this model.

- `modelName`: string — A name for the model.

  **Default:** <auto> If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the model name. For more information, see Name Type.

  Important
   If you specify a name, you cannot perform updates that
   require replacement of this resource. You can perform
   updates that require no or some interruption. If you
   must replace the resource, specify a new name.

- `restApi`: [IRestApi](/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IRestApi.html) (required) — The rest API that this model is part of.

  The reason we need the RestApi object itself and not just the ID is because the model
  is being tracked by the top-level RestApi object for the purpose of calculating its
  hash to determine the ID of the deployment. This allows us to automatically update
  the deployment when the model of the REST API changes.

- `schema`: [JsonSchema](/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.JsonSchema.html) (required) — The schema to use to transform data to one or more output formats.

  Specify null ({}) if you don't want to specify a schema.

## Properties

- `EMPTY_MODEL`: [IModel](/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IModel.html) (static) — Represents a reference to a REST API's Empty model, which is available as part of the model collection by default.

  This can be used for mapping
  JSON responses from an integration to what is returned to a client,
  where strong typing is not required. In the absence of any defined
  model, the Empty model will be used to return the response payload
  unmapped.

  Definition
  {
    "$schema" : "http://json-schema.org/draft-04/schema#",
    "title" : "Empty Schema",
    "type" : "object"
  }

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `ERROR_MODEL`: [IModel](/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IModel.html) (static) — Represents a reference to a REST API's Error model, which is available as part of the model collection by default.

  This can be used for mapping
  error JSON responses from an integration to a client, where a simple
  generic message field is sufficient to map and return an error payload.

  Definition
  {
    "$schema" : "http://json-schema.org/draft-04/schema#",
    "title" : "Error Schema",
    "type" : "object",
    "properties" : {
      "message" : { "type" : "string" }
    }
  }

- `modelId`: string — Returns the model name, such as 'myModel'.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

## Methods

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `fromModelName(scope: Construct, id: string, modelName: string): IModel` (static)

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
