Forum Discussion

fcc_archivist's avatar
fcc_archivist
Copper Contributor
Aug 26, 2025

What's the secret sauce for getting Functions API to work with static web site?

I'm brand new, got my first Azure static web site up and running so that's good!  Now I need to create some images in code and that's fighting me tooth and nail.

The code to generate the image looks like this:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using SkiaSharp;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace Api
{
    public class GenerateImage
    {
        private readonly ILogger _logger;

        public GenerateImage(ILoggerFactory loggerFactory)
        {
            Debug.WriteLine($"GenerateImage.GenerateImage()");

            _logger = loggerFactory.CreateLogger<GenerateImage>();
        }

        // http://localhost:7071/api/image/124 works
        [Function("GenerateImage")]
        public HttpResponseData Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "image/{id}")] HttpRequestData req,
            string id)
        {
            int width = 200, height = 100;

            Debug.WriteLine($"GenerateImage.Run() [id={id}]");

            using var bitmap = new SKBitmap(width, height);
            using var canvas = new SKCanvas(bitmap);
            canvas.Clear(SKColors.LightBlue);

            var paint = new SKPaint
            {
                Color = SKColors.Black,
                TextSize = 24,
                IsAntialias = true
            };

            canvas.DrawText($"ID: {id}", 10, 50, paint);

            using var ms = new MemoryStream();
            bitmap.Encode(ms, SKEncodedImageFormat.Png, 100);
            ms.Position = 0;

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "image/png");
            response.Headers.Add("Cache-Control", "public, max-age=86400"); // 1 day
            // response.Body = ms;
            ms.CopyTo(response.Body);

            return response;
        }
    }
}

and if I navigate to http://localhost:7071/api/image/124 (for example) it happily generates an image with the number 124 in it.

But if I add the HTML tag

<img src="/api/image/123" alt="Generated Image">

to one of my other web pages, it says there's no such page.  Apparently this is because my web pages are coming from my web site and it's at https://localhost:7154 and it doesn't know how to contact the Functions API.

My staticwebapp.config.json looks like this:

{
  "routes": [
    {
      "route": "/api/*",
      "allowedRoles": [ "anonymous" ]
    }
  ],
  "navigationFallback": {
    "rewrite": "/index.html",
    "exclude": [ "/api/*" ]
  }
}

What am I missing?

2 Replies

  • Kidd's managed-functions route is one way in, but there's a second option that's probably the one you want, because your function is already its own project running on localhost:7071 — you don't have to move it into the static site's repo at all.

     

    Your config is fine, by the way. The route rule and the navigationFallback exclusion are both right, so stop fiddling with the JSON — the problem is that nothing is attached behind /api on the deployed site, so the request has nowhere to go. Locally it works because the Functions host is running; on Azure, /api only means something once an API backend is attached to the static web app.

     

    There are two ways to attach one. The first is the managed route Kidd described: an api folder at the root of the repo that gets built and deployed with the site. The second is to link the function app you already have: in the portal, open the static web app, go to Settings and then APIs, and on the Production row choose Link, pick Function App and your function app. From then on, /api/image/123 on your static site's address proxies straight through to the same function — the function app's endpoint needs to keep its /api prefix, since that's what the static app matches on.

     

    Two catches before you do it. Linking needs the Standard plan on the static web app, so if you're on the free tier that's the first thing to check. And if your workflow file has an api_location set, clear it to an empty string first, because a linked backend and a managed api folder can't both be in play at once. If you only need this one site, the managed route is simpler; if the function is meant to serve other things too, linking is the right call.

  • Take the following considerations:

     

    The Secret Sauce: Hosting Together + Correct Routing
    1. Functions Must Be Inside the Static Web App Project
    Azure Static Web Apps expects your API to live in a folder named api at the root of your repository. This is how it knows to bundle and deploy your Functions alongside your static site.
    Your structure should look like:

    /root
      /api
        GenerateImage.cs
        function.json
      /static
        index.html
        other pages...
      staticwebapp.config.json


    If your Functions project is running separately (like on localhost:7071), your static site won’t know how to route to it unless you link it manually or host them together.

    2. Use Azure Static Web Apps CLI for Local Testing
    To simulate the full environment locally (static site + API), use the Azure Static Web Apps CLI (swa).

    npm install -g azure/static-web-apps-cli
    swa start ./static --api ./api


    This will:
    •    Serve your static site
    •    Proxy /api/* requests to your Functions
    •    Run everything on a single port (e.g., http://localhost:4280)
    Now your <img src="/api/image/123"> will work as expected.

    3. Deploying to Azure
    When you deploy to Azure Static Web Apps:
    •    Your GitHub Actions or Azure DevOps pipeline should include both the static site and the API folder
    •    Azure will automatically host the API under /api/* and route requests accordingly