Skip to Content
WearablesDexcom CGM

Dexcom CGM Integration

Dexcom continuous glucose monitors (G6, G7, ONE, ONE+) provide real-time glucose data through the Dexcom Developer API.

OAuth Setup

Prerequisites

  1. Register as a Dexcom developer at developer.dexcom.com 
  2. Create an application in the Dexcom Developer Portal
  3. 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/callback

Supported Devices

DeviceStatus
Dexcom G6Supported
Dexcom G7Supported
Dexcom ONESupported
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_TOKEN

The 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" } }
TrendDescriptionRate (mg/dL/min)
doubleUpRising rapidly> 3.0
singleUpRising2.0–3.0
fortyFiveUpRising slowly1.0–2.0
flatStable-1.0–1.0
fortyFiveDownFalling slowly-2.0 to -1.0
singleDownFalling-3.0 to -2.0
doubleDownFalling rapidly< -3.0

Database Storage

patient_cgm_connections

ColumnTypeDescription
iduuidConnection ID
userIdtextClerk user ID
providertextdexcom or libre
accessTokentextOAuth access token (encrypted)
refreshTokentextOAuth refresh token (encrypted)
expiresAttimestampToken expiration
isActivebooleanConnection status
createdAttimestampCreation time

patient_glucose_readings

ColumnTypeDescription
iduuidReading ID
userIdtextClerk user ID
connectionIduuidCGM connection reference
valuenumericGlucose value (mg/dL)
trendtextGlucose trend direction
recordedAttimestampWhen the reading was taken

Automatic Sync

The syncCgmReadings Trigger.dev job runs every 30 minutes:

  1. Finds all active Dexcom connections
  2. Refreshes tokens if expired
  3. Fetches glucose readings since last sync
  4. Stores readings in patient_glucose_readings
  5. Updates wearable data in Patient Graph

Glucose Ranges

Range (mg/dL)Classification
< 54Severe hypoglycemia
54–69Hypoglycemia
70–99Normal (fasting)
70–140Normal (post-meal)
100–125Prediabetic (fasting)
> 126Diabetic (fasting)
> 180High (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