Azure Static Web App - Bring your own API
source link: https://bartwullems.blogspot.com/2024/03/azure-static-web-app-bring-your-own-api.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Azure Static Web App
Azure Static Web App - Bring your own API
As a follow-up on the presentation I did at CloudBrew about Azure Static Web Apps I want to write a series of blog posts.
In the last post in this series, I explained that with every Static Web App you get a serverless API endpoint (based on Azure Functions) for free. However you have also the option to bring your own API. This can be an Azure Function but also an API exposed through Azure API Management, an Azure App Service or Azure Container Apps.
The advantage of using this feature is that you get:
-
Integrated security with direct access to user authentication and role-based authorization data.
-
Seamless routing that makes the
/api
route available to the front-end web app without requiring custom CORS rules.
In this post I’ll show you how to do this and I’ll use Azure Container Apps to demonstrate it.
The Azure Container App
I already created the Azure Container App(ACA) in Visual Studio and deployed it to Azure. Most important is that the ACA exposes an API listening on the /api
endpoint.
As I mentioned I deployed the ACA. I didn’t add any authentication or authorization so right now anyone can call this api.
Linking our API
Next step is to link the API to our Azure Static Web App. Therefore we go to our Static Web App in the Azure Portal and click on the APIs section:
To link an API we need to click on the Link hyperlink next to the environment.
Remark: The link option will only be available when Azure Static Web App has detected that no existing Managed Function is found inside your source repository.
On the Link new Backend screen, we can select a Resource Type and select the Resource itself. Click on Link to complete the process.
You can see that the 'withoutapi' environment is now linked to our Azure Container App:
Now our ACA API is no longer directly accessible but can only be called through our Azure Static Web App.
If you go to the ACA resource in the Azure Portal, you can see that an Azure Static Web App is used as the Identity Provider.
Calling the API
To call our API we first need to make sure that a corresponding route is configured in our staticwebapp.config.json
file:
Once that is done, we can invoke the API through our frontend code:
More information
Overview of API support in Azure Static Web Apps | Microsoft Learn
Bring your own functions to Azure Static Web Apps | Microsoft Learn
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK