title: “Badge Function (GetScanCount)” layout: default permalink: /docs/badge-function/ —
Azure Function: GetScanCount (NFC Scan Badge)
This Azure Function powers the “Total NFC Scans” badge shown in the README:
It queries Application Insights using the App Insights REST API (read-only) and returns shields.io compatible JSON.
Why a separate function?
- Keeps badge logic isolated from the mobile app and proxy services.
- Allows independent deployment/scaling of the public badge endpoint (anonymous).
- The count is derived from
customEventstelemetry emitted by the MAUI app during NFC scans, provisioning, and inventory operations (seeDeviceService.ScanPhoneAsync+TelemetryClient.TrackEvent).
Required App Settings
| Name | Required | Description |
|---|---|---|
APP_INSIGHTS_APP_ID |
Yes | Application Insights application ID (from the resource used by the mobile app). |
APP_INSIGHTS_API_KEY |
Yes | API key with read permissions for the App Insights resource (create via Azure Portal > API Access). |
Optional App Settings
| Name | Default | Description |
|---|---|---|
APP_INSIGHTS_KQL_QUERY |
customEvents \| count |
Full KQL query that must return a single numeric value in the first cell of the first row. You can embed constants (baselines), filters, time ranges, etc. directly in the query. Example: print 1234 + toscalar(customEvents \| count). |
Error / Configuration States
The endpoint always returns HTTP 200 + valid shields.io JSON so the badge renders reliably in GitHub READMEs.
Visible message values in the badge:
- Normal count (e.g.
1247) unconfigured(light grey) — missingAPP_INSIGHTS_APP_ID/APP_INSIGHTS_API_KEYerror(red) — query failure, bad API key, malformed result shape, parse error, etc.
Logs in the Function App will contain details for troubleshooting.
Deployment Notes
- The Function App is a .NET 9 isolated Azure Functions app (
src/DeviceFX.Function). - It has no dependency on the main mobile app build (excluded from
azure-pipelines.yml). - Deploy via:
- Azure Functions Core Tools (
func azure functionapp publish ...) - Azure Portal / GitHub Actions / Azure DevOps (separate pipeline recommended)
- VS Code Azure Functions extension
- Azure Functions Core Tools (
- Requires the two App Insights read settings above (plus standard
AzureWebJobsStorageandFUNCTIONS_WORKER_RUNTIME).
Local Development
- Copy
src/DeviceFX.Function/local.settings.jsonand fill real values (or keep placeholders to test the “unconfigured” path). - Run the function project (F5 in VS Code or
func startfrom thesrc/DeviceFX.Functiondir afterdotnet build). - Hit
http://localhost:PORT/api/GetScanCount(port shown in launch profile or output).
Updating the Badge Query Without Code Changes
Update the APP_INSIGHTS_KQL_QUERY Application Setting. The query itself can include any baseline or calculation (e.g. print 1234 + toscalar(customEvents | count)). No code change or mobile app rebuild is required.
References
- Source: src/DeviceFX.Function/GetScanCount.cs
- Telemetry source: src/DeviceFX.NfcApp/Services/DeviceService.cs (TrackEvent calls)
- Badge URL in README:
https://img.shields.io/endpoint?url=.../api/GetScanCount - Issue: #14 — Extend scan badge to preserve lifetime total beyond App Insights retention.