The `serverless-add-api-key` plugin extends the Serverless Framework to provide enhanced management of AWS API Gateway API keys and usage plans. Unlike native Serverless functionality, this plugin allows associating multiple services with the same API key and usage plan, facilitating consistent authentication across microservices. It automatically creates API keys and usage plans if they don't exist and supports associating existing keys. The current stable version is 4.2.1, with releases typically following major Serverless Framework updates or when new features for API Gateway integration are required. Key differentiators include its ability to reuse existing keys, support for multiple keys per service, and advanced configuration options for usage plans, including quotas and throttling. It also provides secure handling of API key values through KMS encryption, preventing raw keys from being exposed in repositories.
npm install serverless-add-api-keyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `serverless-add-api-key` to create multiple API keys, including one with a custom usage plan, one with a KMS-encrypted value, and another that defaults to the provider-level usage plan. It shows how to integrate keys with HTTP API Gateway endpoints and use stage-specific naming.
To prevent API key values from being displayed on the console, use the `--conceal` option with the `sls deploy` command (e.g., `sls deploy --conceal`).
Ensure all manually specified API key values adhere to the AWS API Gateway minimum length requirement of 20 characters.
Explicitly define `usagePlan` for each API key in `custom.apiKeys` if you require specific settings. Alternatively, ensure the `provider.usagePlan` section is configured to provide a sensible default for keys without individual usage plan definitions.
Verify that the `encrypted` value is a valid base64-encoded ciphertext produced by AWS KMS and that `kmsKeyRegion` accurately points to the region where the KMS key resides. Ensure the deployment role has permissions to decrypt with the specified KMS key.
Update the API key `value` to be at least 20 characters long.
Provide the `kmsKeyRegion` explicitly in the API key configuration (e.g., `kmsKeyRegion: us-east-1`) or ensure an AWS region is configured in your deployment environment.
Review your IAM role/user policies and ensure they include permissions for `apigateway:CreateApiKey`, `apigateway:UpdateApiKey`, `apigateway:CreateUsagePlan`, `apigateway:UpdateUsagePlan`, `apigateway:GET_API_KEYS`, `apigateway:GET_USAGE_PLANS`, and if using KMS, `kms:Decrypt` for the relevant KMS key.