Overview
The Tailscale API Edge Function provides integration with Tailscale’s network, enabling device discovery, auth key generation, enrollment validation, and device synchronization. Endpoint:/functions/v1/tailscale-api?action={action}
Authentication: Required (Authorization header with Supabase JWT for most actions)
Method: POST for most actions, GET for list operations
Authentication
The function uses OAuth 2.0 client credentials flow to obtain Tailscale API access tokens:Actions
List Devices
Retrieve all devices in the Tailscale network (tailnet). Action:list-devices
Authentication: Service-level only
Request success status
Array of Tailscale devices
Example
Check Device
Check if a specific device exists in the Tailscale network. Action:check-device
Device hostname, name, or IP address to search for
Request success
Whether device was found
Device online status (if found)
Device details (if found)
Example
Generate Auth Key
Generate a pre-authorized Tailscale auth key for device enrollment. Action:generate-auth-key
Authentication: User-level required
Device ID to associate with auth key
Tailscale ACL tags to apply (default:
["tag:prod"])Tailscale ACL group (default:
"sap")Whether key was generated
Tailscale auth key for device enrollment
Key expiration timestamp (24 hours)
Example
Validate Enrollment
Validate that a device has successfully enrolled in Tailscale. Action:validate-enrollment
Authentication: User-level required
Device ID to validate
Expected hostname to match in Tailscale
Request success
Whether device is enrolled and online
Tailscale device details (if enrolled)
Status message
Example
Sync Devices
Synchronize all Tailscale devices with local database. Action:sync-devices
Authentication: User-level required
Sync success
Number of devices synchronized
Total devices in Tailscale network
Example
TypeScript Interfaces
TypeScript
OAuth Flow
The function automatically handles OAuth token acquisition:- Requests access token from
https://api.tailscale.com/api/v2/oauth/token - Uses client credentials grant type
- Caches token for subsequent requests
- Automatically determines tailnet from token
OAuth Request
Auth Key Configuration
When generating auth keys, the function:- Prefers stored key: Uses
TAILSCALE_AUTH_KEYenvironment variable if available - Fallback to API: Generates new key via Tailscale API if no stored key
- Key properties:
- Reusable: Yes
- Ephemeral: No (persistent devices)
- Pre-authorized: Yes (no manual approval needed)
- Expiry: 24 hours
Device Metadata Updates
When devices enroll or sync, thedevices table is updated with:
Device Metadata
Device Events
Logged todevice_events table:
tailscale_auth_key_generated- Auth key createdtailscale_enrollment_complete- Device enrolled successfullytailscale_sync- Device status synchronized
Error Handling
Error message
Tailscale OAuth credentials not configured- Missing client ID/secretFailed to obtain Tailscale access token- OAuth failureDevice not found in Tailscale network- Device hasn’t enrolledFailed to generate Tailscale auth key- API key generation failedAuthorization required- Missing auth headerInvalid authentication- Invalid JWT
API Endpoints Used
The function interacts with these Tailscale API endpoints:POST /api/v2/oauth/token- OAuth token acquisitionGET /api/v2/whoami- Get tailnet informationGET /api/v2/tailnet/{tailnet}/devices- List devicesGET /api/v2/device/{deviceId}- Get device detailsPOST /api/v2/tailnet/{tailnet}/keys- Generate auth keys
Related Tables
devices- Local device recordsdevice_events- Device activity logorganization_tailscale_config- Per-org Tailscale configurationaudit_logs- API call audit trail