Manually tracking workout routines in Notion can be tedious and prone to inconsistencies. This automation ensures that workout entries are consistently and efficiently duplicated from predefined templates, reducing manual input while maintaining accuracy.
- Trigger: A request with
workout_id
is sent to the Cloudflare Worker. - Template Fetching: The worker retrieves the workout template linked to the given
workout_id
. - Entry Duplication: Exercises from the template are duplicated into the workout entry database.
- Batch Processing: API calls are batched (3 per request) with adaptive delays to avoid rate limits.
- Logging: Successes and failures are logged both in console and a Notion Inbox database (with rate-controlled logging).
workoutService.ts
: Handles fetching templates, processing entries, and batching Notion API calls.notionService.ts
: Fetches Notion data and normalizes IDs.notionClient.ts
: Wrapper for Notion API interactions with error handling and logging.logger.ts
: Logs API interactions with truncation and controlled frequency.
{
"Workout Template": { "relation": [{ "id": "template_id" }] },
"Exercises": { "relation": [{ "id": "exercise_id" }] },
"Reps": { "number": 10 },
"Weight": { "number": 50 },
"Set #": { "rich_text": [{ "text": { "content": "Set 1" } }] }
}
{
"Workout": { "relation": [{ "id": "workout_id" }] },
"Exercises": { "relation": [{ "id": "exercise_id" }] },
"Reps": { "number": 10 },
"Weight": { "number": 50 },
"Set #": { "rich_text": [{ "text": { "content": "Set 1" } }] }
}
- Set environment variables:
NOTION_API_KEY
: Your Notion integration token.WORKOUT_ENTRY_TEMPLATES_DB_ID
: Notion DB ID for workout templates.WORKOUT_ENTRIES_DB_ID
: Notion DB ID for duplicated entries.NOTION_INBOX_DB_ID
: Notion DB ID for logging interactions.
- Deploy to Cloudflare Workers.
Use wrangler tail
to monitor logs:
wrangler tail --service notion-api
- Rate Limits? Reduce batch size or increase delay.
- Logging Errors? Notion API has a 2000-char limit; messages auto-truncate.
- Missing Entries? Ensure templates are properly linked in Notion.
- Use Cloudflare KV for caching templates.
- Optimize batch handling with exponential backoff.