Search code examples
pythonamazon-web-servicesboto3aws-api-gateway

Associate a usage plan to api key using boto3 AWS api gateway


Hey I am using boto3 library to create a new api key. I successfully created a new api key but I am having trouble associate it with the existing usage plan. Can you please help me to associate api-key to existing usage plan My code to create api key

import boto3
apiclient= boto3.client('apigateway')
response= apiclient.create_api_key(
        name='string',
        enabled=True,
        stageKeys=[{
            'restApiId':'string',
            'stageName':'string'
        }]
        )

Solution

  • I was to able to attach usage plan to api key with create_usage_plan_key() method Implementation:

    import boto3
    apiclient= boto3.client('apigateway')
    response= apiclient.create_api_key(
            name='string',
            enabled=True,
            stageKeys=[{
                'restApiId':'string',
                'stageName':'string'
            }]
            )
    api_key_id=response['id']
    usage_planId="<Your usage plan id>"
    keytype='API_KEY' 
    
    usageresponse= apiclient.create_usage_plan_key(
         usagePlanId= usage_planId,
         keyId=api_key_id,
         keyType=keytype
         )
    

    For more information, go to this documentation https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/apigateway.html#APIGateway.Client.create_usage_plan_key