Dexcom CGM Integration
Dexcom continuous glucose monitors (G6, G7, ONE, ONE+) provide real-time glucose data through the Dexcom Developer API.
OAuth Setup
Prerequisites
- Register as a Dexcom developer at developer.dexcom.com
- Create an application in the Dexcom Developer Portal
- Set the redirect URI to
https://my.loop.health/api/cgm/dexcom/callback
Environment Variables
DEXCOM_CLIENT_ID=your-dexcom-client-id
DEXCOM_CLIENT_SECRET=your-dexcom-secret
DEXCOM_REDIRECT_URI=https://my.loop.health/api/cgm/dexcom/callbackSupported Devices
| Device | Status |
|---|---|
| Dexcom G6 | Supported |
| Dexcom G7 | Supported |
| Dexcom ONE | Supported |
| Dexcom ONE+ | Supported |
Connection Flow
Step 1: Initiate Connection
curl -X GET "https://my.loop.health/api/cgm/dexcom/connect" \
-H "Authorization: Bearer $CLERK_JWT"Redirects the user to the Dexcom OAuth authorization page. The getDexcomAuthUrl() function constructs the authorization URL.
Step 2: OAuth Callback
After authorization, Dexcom redirects to:
GET /api/cgm/dexcom/callback?code=AUTH_CODE&state=STATE_TOKENThe server exchanges the authorization code for access and refresh tokens, creates a CGM connection record in the database.
Step 3: Start Receiving Data
CGM data is synced automatically via Trigger.dev every 30 minutes.
Data Collected
Glucose Readings
{
"metricType": "glucose",
"source": "dexcom",
"metrics": {
"value": 105,
"unit": "mg/dL",
"trend": "flat",
"trendRate": 0.5,
"transmitterGeneration": "G7"
}
}Glucose Trends
| Trend | Description | Rate (mg/dL/min) |
|---|---|---|
doubleUp | Rising rapidly | > 3.0 |
singleUp | Rising | 2.0–3.0 |
fortyFiveUp | Rising slowly | 1.0–2.0 |
flat | Stable | -1.0–1.0 |
fortyFiveDown | Falling slowly | -2.0 to -1.0 |
singleDown | Falling | -3.0 to -2.0 |
doubleDown | Falling rapidly | < -3.0 |
Database Storage
patient_cgm_connections
| Column | Type | Description |
|---|---|---|
id | uuid | Connection ID |
userId | text | Clerk user ID |
provider | text | dexcom or libre |
accessToken | text | OAuth access token (encrypted) |
refreshToken | text | OAuth refresh token (encrypted) |
expiresAt | timestamp | Token expiration |
isActive | boolean | Connection status |
createdAt | timestamp | Creation time |
patient_glucose_readings
| Column | Type | Description |
|---|---|---|
id | uuid | Reading ID |
userId | text | Clerk user ID |
connectionId | uuid | CGM connection reference |
value | numeric | Glucose value (mg/dL) |
trend | text | Glucose trend direction |
recordedAt | timestamp | When the reading was taken |
Automatic Sync
The syncCgmReadings Trigger.dev job runs every 30 minutes:
- Finds all active Dexcom connections
- Refreshes tokens if expired
- Fetches glucose readings since last sync
- Stores readings in
patient_glucose_readings - Updates wearable data in Patient Graph
Glucose Ranges
| Range (mg/dL) | Classification |
|---|---|
| < 54 | Severe hypoglycemia |
| 54–69 | Hypoglycemia |
| 70–99 | Normal (fasting) |
| 70–140 | Normal (post-meal) |
| 100–125 | Prediabetic (fasting) |
| > 126 | Diabetic (fasting) |
| > 180 | High (post-meal) |
Luna AI Integration
Luna AI can access CGM data through the getWearableInsights tool with metricType: 'glucose'. It provides:
- Average glucose over time periods
- Time in range (TIR) analysis
- Glucose variability (coefficient of variation)
- Trend analysis and recommendations
- Correlation with protocol adherence