|
| 1 | +# Azure API Management Guide for FinBin Backend |
| 2 | + |
| 3 | +## What is API Management? |
| 4 | + |
| 5 | +Azure API Management (APIM) is a complete solution for publishing, securing, transforming, maintaining, and monitoring APIs. For your FinBin application, it sits as a façade between your backend services and API consumers. |
| 6 | + |
| 7 | +## Getting Started with API Management |
| 8 | + |
| 9 | +### 1. Create an API Management Service |
| 10 | + |
| 11 | +1. In Azure Portal, go to **Create a resource** > **Integration** > **API Management** |
| 12 | +2. Fill in the basics: |
| 13 | + - **Name**: `finbin-apim` (must be unique) |
| 14 | + - **Subscription**: Your subscription |
| 15 | + - **Resource Group**: Use the same as your App Service (`rg-foundrysandbox`) |
| 16 | + - **Location**: Same region as your App Service |
| 17 | + - **Organization name**: Your organization |
| 18 | + - **Administrator email**: Your email |
| 19 | + - **Pricing tier**: Developer (for testing) or Basic/Standard (for production) |
| 20 | + |
| 21 | +### 2. Import Your FinBin API |
| 22 | + |
| 23 | +#### Option A: Import from OpenAPI (Swagger) |
| 24 | + |
| 25 | +1. Go to your API Management service |
| 26 | +2. Select **APIs** > **+ Add API** > **OpenAPI** |
| 27 | +3. Enter the URL to your Swagger endpoint: `https://finbinbackend.azurewebsites.net/swagger/v1/swagger.json` |
| 28 | + - **Note**: This URL must be publicly accessible for API Management to import it |
| 29 | +4. Configure: |
| 30 | + - **Display name**: `FinBin API` |
| 31 | + - **Name**: `finbin-api` |
| 32 | + - **API URL suffix**: `finbin` |
| 33 | + - **Base URL**: Your backend URL |
| 34 | + - **Version**: `v1` |
| 35 | + |
| 36 | +#### Option B: Import from App Service |
| 37 | + |
| 38 | +1. Go to your API Management service |
| 39 | +2. Select **APIs** > **+ Add API** > **App Service** |
| 40 | +3. Select your App Service (`finbinbackend`) |
| 41 | +4. Configure similar settings as above |
| 42 | + |
| 43 | +### 2.1 Configure API Definition (Swagger/OpenAPI) |
| 44 | + |
| 45 | +For API Management to properly discover and import your API: |
| 46 | + |
| 47 | +1. **Make sure your Swagger endpoint is publicly accessible**: |
| 48 | + - In App Service, ensure that the Swagger endpoint doesn't require authentication |
| 49 | + - Temporarily disable any IP restrictions that might block APIM from accessing it |
| 50 | + - Test the URL in an incognito browser to verify it's publicly accessible |
| 51 | + |
| 52 | +2. **Set up CORS for your Swagger endpoint** (if needed): |
| 53 | + ```csharp |
| 54 | + app.UseSwagger(c => { |
| 55 | + c.PreSerializeFilters.Add((swaggerDoc, httpReq) => { |
| 56 | + swaggerDoc.Servers = new List<OpenApiServer> { |
| 57 | + new OpenApiServer { Url = $"https://{httpReq.Host.Value}" } |
| 58 | + }; |
| 59 | + }); |
| 60 | + }); |
| 61 | + ``` |
| 62 | + |
| 63 | +3. **Configure API definition in APIM**: |
| 64 | + - Go to your API in APIM |
| 65 | + - Select **Settings** tab |
| 66 | + - Under **API definition**, choose "OpenAPI" format |
| 67 | + - Enter your Swagger URL or paste the JSON content directly |
| 68 | + - Click "Save" to update the definition |
| 69 | + |
| 70 | +4. **Keep your Swagger documentation synchronized**: |
| 71 | + - Set up automatic import from your Swagger endpoint |
| 72 | + - In your API settings, enable "Always use latest API specification" |
| 73 | + - Configure a schedule for automatic updates |
| 74 | + |
| 75 | +5. **Add API definition to Developer Portal**: |
| 76 | + - Enable the Developer Portal |
| 77 | + - Make sure API documentation is enabled |
| 78 | + - Customize the documentation appearance |
| 79 | + |
| 80 | +### 3. Configure Security |
| 81 | + |
| 82 | +#### Set Up JWT Validation |
| 83 | + |
| 84 | +1. Go to your API > **Settings** |
| 85 | +2. In the **Security** tab, add a JWT validation policy: |
| 86 | + ```xml |
| 87 | + <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized"> |
| 88 | + <openid-config url="your-auth-metadata-endpoint" /> |
| 89 | + <audiences> |
| 90 | + <audience>same-as-your-JWT-audience</audience> |
| 91 | + </audiences> |
| 92 | + <issuers> |
| 93 | + <issuer>same-as-your-JWT-issuer</issuer> |
| 94 | + </issuers> |
| 95 | + </validate-jwt> |
| 96 | + ``` |
| 97 | + |
| 98 | +### 4. Configure Policies |
| 99 | + |
| 100 | +At the API level, add these important policies: |
| 101 | + |
| 102 | +1. **CORS Policy** (if needed): |
| 103 | + ```xml |
| 104 | + <cors allow-credentials="true"> |
| 105 | + <allowed-origins> |
| 106 | + <origin>https://your-frontend-url.azurewebsites.net</origin> |
| 107 | + </allowed-origins> |
| 108 | + <allowed-methods preflight-result-max-age="300"> |
| 109 | + <method>GET</method> |
| 110 | + <method>POST</method> |
| 111 | + <method>PUT</method> |
| 112 | + <method>DELETE</method> |
| 113 | + </allowed-methods> |
| 114 | + <allowed-headers> |
| 115 | + <header>*</header> |
| 116 | + </allowed-headers> |
| 117 | + </cors> |
| 118 | + ``` |
| 119 | + |
| 120 | +2. **Rate Limiting**: |
| 121 | + ```xml |
| 122 | + <rate-limit calls="20" renewal-period="60" /> |
| 123 | + ``` |
| 124 | + |
| 125 | +3. **Caching** (for appropriate endpoints): |
| 126 | + ```xml |
| 127 | + <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" downstream-caching-type="none" /> |
| 128 | + <cache-store duration="60" /> |
| 129 | + ``` |
| 130 | + |
| 131 | +### 5. Set Up Products |
| 132 | + |
| 133 | +1. Create a Product (e.g., "FinBin Standard") |
| 134 | +2. Add your API to the product |
| 135 | +3. Configure access control (subscription required or open) |
| 136 | +4. Publish the product |
| 137 | + |
| 138 | +### 6. Configure Diagnostics and Monitoring |
| 139 | + |
| 140 | +1. Go to **Diagnostics** in your API Management |
| 141 | +2. Create a new diagnostic setting |
| 142 | +3. Send logs to: |
| 143 | + - Application Insights (same one used by your App Service) |
| 144 | + - Log Analytics Workspace |
| 145 | + |
| 146 | +### 7. Connect to Backend with Managed Identity |
| 147 | + |
| 148 | +1. Enable System Assigned Managed Identity for your API Management |
| 149 | +2. Grant necessary permissions to access: |
| 150 | + - Azure SQL Database |
| 151 | + - Azure OpenAI service |
| 152 | +3. Configure backend with Managed Identity authentication: |
| 153 | + ```xml |
| 154 | + <authentication-managed-identity resource="https://management.azure.com/" /> |
| 155 | + ``` |
| 156 | + |
| 157 | +### 8. Developer Portal (Optional) |
| 158 | + |
| 159 | +1. Go to **Developer Portal** > **Portal Overview** |
| 160 | +2. Customize branding and content |
| 161 | +3. Publish the portal |
| 162 | + |
| 163 | +## Testing Your API |
| 164 | + |
| 165 | +1. Go to **APIs** > Your API > **Test** tab |
| 166 | +2. Select an operation |
| 167 | +3. Configure any parameters |
| 168 | +4. Add Authorization header if required |
| 169 | +5. Click **Send** |
| 170 | + |
| 171 | +## Best Practices for FinBin |
| 172 | + |
| 173 | +1. **Layer Security**: |
| 174 | + - Use API keys for frontend applications |
| 175 | + - Use OAuth/JWT for user authentication |
| 176 | + - Use Managed Identity for backend-to-backend communication |
| 177 | + |
| 178 | +2. **Implement Versioning**: |
| 179 | + - Use URL path versioning (/v1/, /v2/) |
| 180 | + - Use API versioning in APIM |
| 181 | + |
| 182 | +3. **Set Up Monitoring**: |
| 183 | + - Monitor API usage and performance |
| 184 | + - Set up alerts for error thresholds |
| 185 | + - Review logs regularly |
| 186 | + |
| 187 | +4. **Deploy Changes Safely**: |
| 188 | + - Use separate instances for dev/test/prod |
| 189 | + - Use revisions for non-breaking changes |
| 190 | + - Use versions for breaking changes |
| 191 | + |
| 192 | +## Next Steps |
| 193 | + |
| 194 | +1. Set up a custom domain for your API Management |
| 195 | +2. Implement advanced policies (transformation, validation) |
| 196 | +3. Configure mutual TLS for backend communication |
| 197 | +4. Set up a CI/CD pipeline for API Management using Azure DevOps or GitHub Actions |
0 commit comments