# Custom Backend Sync Logic

## 📌 Idea
Sync notes to a custom backend (`http://172.16.2.210:5230/`) using the existing Clerk token or access token, alongside the main API.

## 🗂 Codebase Scope
- **Root:** `./`
- **Total files:** ~5 files (api-client.ts, service-worker.ts, Settings UI, etc.)
- **Affected:** 3-5 files
- **Languages:** TypeScript
- **Size:** S

## 📊 Current State Analysis
Currently, `cuccu note` synchronizes memos to a configured `apiBaseUrl` (default `http://160.191.50.138:3001`) via `api-client.ts` using Clerk JWT auth. 
There's no native mechanism to dual-write or sync to an alternative secondary endpoint out of the box when capturing a web highlight.

## 🎯 Goal / Definition of Done
- Provide a setting or hardcode the ability to sync saved notes to `http://172.16.2.210:5230/`.
- Authenticate the request using the active token (fresh token).
- Trigger this sync flow seamlessly when the extension "notes something interesting" (saves a memo).

## 📋 Execution Plan

### Phase 1: API Configuration
- [ ] Add `syncApiUrl` to the Chrome local storage and Settings component (defaulting to `http://172.16.2.210:5230/` or as configured).

### Phase 2: Dual Write Logic
- [ ] Update `createMemo` in `api-client.ts` or the `SAVE_NOTE` handler in `service-worker.ts` to also push the payload to the secondary API.
- [ ] Use `apiHeaders()` to append the active Bearer token to the secondary request.

### Phase 3: Validation
- [ ] Verify that saving a note correctly POSTs to both the primary API and the `172.16.2.210` endpoint without blocking the UI if the secondary fails.

## ⚠️ Risks & Side Effects
- Dual-writing could increase latency if not done asynchronously.
- The secondary server must support the same or compatible data schema and auth strategy as the primary one.

## 🔧 Tools / Scripts Needed
- Code changes in `src/shared/api-client.ts`, `src/background/service-worker.ts`.

## ⏱ Effort Estimate
**S** — < 2 hours, ~3-4 files.

---
_Captured: 2026-04-28 | Type: Codebase-Aware_
