aws-cdk-lib.aws_apigateway.ApiKey

class ApiKey (construct)

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

Implements IConstruct, IDependable, IResource, IApiKey

An API Gateway ApiKey.

An ApiKey can be distributed to API clients that are executing requests for Method resources that require an Api Key.

Example

const importedKey = apigateway.ApiKey.fromApiKeyId(this, 'imported-key', '<api-key-id>');

Initializer

new ApiKey(scope: Construct, id: string, props?: ApiKeyProps)

Parameters

  • scope Construct
  • id string
  • props ApiKeyProps

Construct Props

NameTypeDescription
apiKeyName?stringA name for the API key.
customerId?stringAn AWS Marketplace customer identifier to use when integrating with the AWS SaaS Marketplace.
defaultCorsPreflightOptions?CorsOptionsAdds a CORS preflight OPTIONS method to this resource and all child resources.
defaultIntegration?IntegrationAn integration to use as a default for all methods created within this API unless an integration is specified.
defaultMethodOptions?MethodOptionsMethod options to use as a default for all methods created within this API unless custom options are specified.
description?stringA description of the purpose of the API key.
enabled?booleanIndicates whether the API key can be used by clients.
generateDistinctId?booleanSpecifies whether the key identifier is distinct from the created API key value.
resources?⚠️IRestApi[]A list of resources this api key is associated with.
stages?IStage[]A list of Stages this api key is associated with.
value?stringThe value of the API key.

apiKeyName?

Type: string (optional, default: automically generated name)

A name for the API key.

If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the API key name.


customerId?

Type: string (optional, default: none)

An AWS Marketplace customer identifier to use when integrating with the AWS SaaS Marketplace.


defaultCorsPreflightOptions?

Type: CorsOptions (optional, default: CORS is disabled)

Adds a CORS preflight OPTIONS method to this resource and all child resources.

You can add CORS at the resource-level using addCorsPreflight.


defaultIntegration?

Type: Integration (optional, default: Inherited from parent.)

An integration to use as a default for all methods created within this API unless an integration is specified.


defaultMethodOptions?

Type: MethodOptions (optional, default: Inherited from parent.)

Method options to use as a default for all methods created within this API unless custom options are specified.


description?

Type: string (optional, default: none)

A description of the purpose of the API key.


enabled?

Type: boolean (optional, default: true)

Indicates whether the API key can be used by clients.


generateDistinctId?

Type: boolean (optional, default: false)

Specifies whether the key identifier is distinct from the created API key value.


resources?⚠️

⚠️ Deprecated: - use stages instead

Type: IRestApi[] (optional, default: none)

A list of resources this api key is associated with.


stages?

Type: IStage[] (optional, default: the api key is not associated with any stages)

A list of Stages this api key is associated with.


value?

Type: string (optional, default: none)

The value of the API key.

Must be at least 20 characters long.

Properties

NameTypeDescription
envResourceEnvironmentThe environment this resource belongs to.
keyArnstringThe API key ARN.
keyIdstringThe API key ID.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.

env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


keyArn

Type: string

The API key ARN.


keyId

Type: string

The API key ID.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.

Methods

NameDescription
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
grantRead(grantee)Permits the IAM principal all read operations through this key.
grantReadWrite(grantee)Permits the IAM principal all read and write operations through this key.
grantWrite(grantee)Permits the IAM principal all write operations through this key.
toString()Returns a string representation of this construct.
static fromApiKeyId(scope, id, apiKeyId)Import an ApiKey by its Id.

applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

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).


grantRead(grantee)

public grantRead(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — The principal to grant access to.

Returns

  • Grant

Permits the IAM principal all read operations through this key.


grantReadWrite(grantee)

public grantReadWrite(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — The principal to grant access to.

Returns

  • Grant

Permits the IAM principal all read and write operations through this key.


grantWrite(grantee)

public grantWrite(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — The principal to grant access to.

Returns

  • Grant

Permits the IAM principal all write operations through this key.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromApiKeyId(scope, id, apiKeyId)

public static fromApiKeyId(scope: Construct, id: string, apiKeyId: string): IApiKey

Parameters

  • scope Construct
  • id string
  • apiKeyId string

Returns

  • IApiKey

Import an ApiKey by its Id.