Integrate Dexcom in ASP.NET Core Web API

Author: Dasan | Date: April 11, 2025

1. 🧠 Introduction

Dexcom provides continuous glucose monitoring (CGM) data. By integrating Dexcom's API with ASP.NET Core Web API, you can access real-time glucose values and trends, empowering healthcare or fitness applications with life-changing data insights.

2. βš™οΈ Prerequisites

3. 🧾 Required Scopes

Dexcom API uses OAuth2 for secure access. The most common scope is:

offline_access
read

4. πŸ” Redirect to Dexcom for Authorization

public IActionResult DexcomLogin()
{
    var clientId = "YOUR_CLIENT_ID";
    var redirectUri = "https://yourdomain.com/dexcom/callback";
    var scope = "offline_access read";
    var authUrl = $"https://sandbox-api.dexcom.com/v2/oauth2/login" +
                  $"?client_id={clientId}&redirect_uri={redirectUri}" +
                  $"&response_type=code&scope={scope}";

    return Redirect(authUrl);
}

5. πŸ” Exchange Code for Access Token

public async Task<OAuthToken> ExchangeDexcomCodeAsync(string code)
{
    var client = new HttpClient();
    var parameters = new Dictionary<string, string>
    {
        { "client_id", "YOUR_CLIENT_ID" },
        { "client_secret", "YOUR_CLIENT_SECRET" },
        { "code", code },
        { "grant_type", "authorization_code" },
        { "redirect_uri", "https://yourdomain.com/dexcom/callback" }
    };

    var response = await client.PostAsync(
        "https://sandbox-api.dexcom.com/v2/oauth2/token",
        new FormUrlEncodedContent(parameters)
    );

    var content = await response.Content.ReadAsStringAsync();
    return JsonSerializer.Deserialize<OAuthToken>(content);
}

6. πŸ“ˆ Get Real-Time Glucose Data

Example: Get latest EGV (Estimated Glucose Value)

public async Task<string> GetLatestGlucoseAsync(string accessToken)
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

    var uri = "https://sandbox-api.dexcom.com/v2/users/self/egvs?startDate=2025-04-10T00:00:00&endDate=2025-04-11T00:00:00";

    var response = await client.GetAsync(uri);
    return await response.Content.ReadAsStringAsync();
}

7. 🧾 Token Model

public class OAuthToken
{
    [JsonPropertyName("access_token")]
    public string AccessToken { get; set; }

    [JsonPropertyName("refresh_token")]
    public string RefreshToken { get; set; }

    [JsonPropertyName("expires_in")]
    public int ExpiresIn { get; set; }

    [JsonPropertyName("token_type")]
    public string TokenType { get; set; }
}

8. πŸ”„ Refresh Token Endpoint

public async Task<string> RefreshDexcomTokenAsync(string refreshToken)
{
    var client = new HttpClient();
    var values = new Dictionary<string, string>
    {
        { "client_id", "YOUR_CLIENT_ID" },
        { "client_secret", "YOUR_CLIENT_SECRET" },
        { "refresh_token", refreshToken },
        { "grant_type", "refresh_token" }
    };

    var response = await client.PostAsync(
        "https://sandbox-api.dexcom.com/v2/oauth2/token",
        new FormUrlEncodedContent(values)
    );

    return await response.Content.ReadAsStringAsync();
}

9. πŸ“Š Dexcom Data Types You Can Access

10. πŸ§ͺ Testing & Sandbox Access

Use the Dexcom Sandbox API to simulate responses before deploying live.

11. 🏁 Conclusion

Integrating Dexcom into your ASP.NET Core Web API allows users to securely connect their CGM devices and receive powerful, actionable insights into their glucose patterns. Ideal for health dashboards, alerts, and patient monitoring systems.

Next blog: Let’s look at integrating Apple HealthKit into your backend or bridging it with Health Connect. Stay tuned! 🍏