Author: Dasan | Date: April 11, 2025
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.
Dexcom API uses OAuth2 for secure access. The most common scope is:
offline_access
read
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);
}
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);
}
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();
}
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; }
}
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();
}
Use the Dexcom Sandbox API to simulate responses before deploying live.
https://sandbox-api.dexcom.comIntegrating 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! π