--- name: azure-api-management description: API gateway and management with Azure API Management. Configure policies, rate limiting, authentication, and developer portal. Use for API lifecycle management, gateway patterns, and API security on Azure. --- # Azure API Management Expert guidance for API gateway and management on Azure. ## Create Instance ```bash # Create APIM instance az apim create \ --name myapim \ --resource-group myResourceGroup \ --location eastus \ --publisher-email admin@contoso.com \ --publisher-name Contoso \ --sku-name Developer # Create API from OpenAPI az apim api import \ --resource-group myResourceGroup \ --service-name myapim \ --path myapi \ --specification-format OpenApiJson \ --specification-url https://api.example.com/openapi.json ``` ## Policies ### Inbound Policies ```xml https://myapp.com GET POST {client-id} ``` ### Transformation Policies ```xml @(Guid.NewGuid().ToString()) 2023-01-01 @{ var response = context.Response.Body.As(); response["timestamp"] = DateTime.UtcNow.ToString("o"); return response.ToString(); } ``` ### Backend Policies ```xml @{ return new JObject( new JProperty("error", context.LastError.Message), new JProperty("requestId", context.RequestId) ).ToString(); } ``` ## Caching ```xml Accept version ``` ## Authentication ### OAuth 2.0 ```xml api://myapi https://sts.windows.net/{tenant}/ ``` ### Subscription Key ```xml ``` ### Managed Identity to Backend ```xml ``` ## Products and Subscriptions ```bash # Create product az apim product create \ --resource-group myResourceGroup \ --service-name myapim \ --product-id premium \ --display-name "Premium" \ --description "Premium API access" \ --subscription-required true \ --approval-required true # Add API to product az apim product api add \ --resource-group myResourceGroup \ --service-name myapim \ --product-id premium \ --api-id myapi # Create subscription az apim subscription create \ --resource-group myResourceGroup \ --service-name myapim \ --display-name "My App Subscription" \ --scope /products/premium ``` ## Named Values ```bash # Create named value az apim nv create \ --resource-group myResourceGroup \ --service-name myapim \ --named-value-id backend-url \ --display-name "Backend URL" \ --value "https://backend.example.com" # Reference in policy # {{backend-url}} ``` ## Bicep Deployment ```bicep resource apim 'Microsoft.ApiManagement/service@2023-03-01-preview' = { name: apimName location: location sku: { name: 'Developer' capacity: 1 } properties: { publisherEmail: publisherEmail publisherName: publisherName } } resource api 'Microsoft.ApiManagement/service/apis@2023-03-01-preview' = { parent: apim name: 'myapi' properties: { displayName: 'My API' path: 'myapi' protocols: ['https'] serviceUrl: 'https://backend.example.com' } } resource policy 'Microsoft.ApiManagement/service/apis/policies@2023-03-01-preview' = { parent: api name: 'policy' properties: { format: 'xml' value: loadTextContent('policy.xml') } } ``` ## Resources - [API Management Documentation](https://learn.microsoft.com/azure/api-management/) - [Policy Reference](https://learn.microsoft.com/azure/api-management/api-management-policies) - [Best Practices](https://learn.microsoft.com/azure/api-management/api-management-howto-use-azure-monitor)