11# API Routes Documentation
22
3+ ## Folder Structure
4+
5+ All API route handlers live under ` app/api/ ` following Next.js 14 App Router conventions.
6+
7+ ```
8+ app/api/
9+ ├── health/
10+ │ └── route.ts # GET /api/health — public health check
11+ ├── auth/
12+ │ ├── nonce/
13+ │ │ └── route.ts # POST /api/auth/nonce
14+ │ ├── login/
15+ │ │ └── route.ts # POST /api/auth/login
16+ │ └── logout/
17+ │ └── route.ts # POST /api/auth/logout
18+ ├── user/
19+ │ └── profile/
20+ │ └── route.ts # GET /api/user/profile
21+ ├── split/
22+ │ ├── route.ts # GET, POST /api/split
23+ │ └── calculate/
24+ │ └── route.ts # GET /api/split/calculate
25+ ├── goals/
26+ │ └── route.ts # GET, POST /api/goals
27+ ├── bills/
28+ │ └── route.ts # GET, POST /api/bills
29+ ├── insurance/
30+ │ └── route.ts # GET, POST /api/insurance
31+ ├── family/
32+ │ └── route.ts # GET, POST /api/family
33+ ├── send/
34+ │ └── route.ts # POST /api/send
35+ ├── remittance/
36+ │ └── route.ts # Remittance endpoints
37+ ├── anchor/
38+ │ └── route.ts # Anchor platform integration
39+ └── webhooks/
40+ └── route.ts # Webhook handlers
41+ ```
42+
43+ ### Naming Convention
44+
45+ ```
46+ app/api/[domain]/[action]/route.ts
47+ ```
48+
49+ - ** domain** — the feature area (e.g. ` split ` , ` goals ` , ` bills ` )
50+ - ** action** — optional sub-action (e.g. ` calculate ` , ` pay ` , ` cancel ` )
51+ - ** route.ts** — exports named functions: ` GET ` , ` POST ` , ` PUT ` , ` DELETE `
52+
53+ ### How to Call an Endpoint
54+
55+ ``` bash
56+ # Health check (public)
57+ curl http://localhost:3000/api/health
58+
59+ # Protected route (requires session cookie)
60+ curl http://localhost:3000/api/split \
61+ -H " Cookie: remitwise-session=<your-session-token>"
62+ ```
63+
64+ ### How to Add a New Route
65+
66+ 1 . Create the folder: ` app/api/[domain]/[action]/ `
67+ 2 . Create ` route.ts ` inside it
68+ 3 . Export the HTTP methods you need:
69+
70+ ``` typescript
71+ import { NextResponse } from " next/server" ;
72+
73+ export async function GET() {
74+ return NextResponse .json ({ data: " example" }, { status: 200 });
75+ }
76+
77+ export async function POST(request : Request ) {
78+ const body = await request .json ();
79+ return NextResponse .json ({ received: body }, { status: 201 });
80+ }
81+ ```
82+
83+ 4 . If the route is protected, wrap with ` withAuth ` (see Authentication section below).
84+
85+ ---
86+
387## Authentication
488
589All API routes use cookie-based session authentication. The session cookie is set after successful login and verified on protected routes.
@@ -23,6 +107,8 @@ async function handler(request: NextRequest, session: string) {
23107export const GET = withAuth (handler );
24108```
25109
110+ ---
111+
26112## Route Classification
27113
28114### Public Routes (No Authentication Required)
@@ -54,6 +140,8 @@ All protected routes return `401 Unauthorized` if no valid session exists.
54140| POST | ` /api/family ` | Add family member |
55141| POST | ` /api/send ` | Send money transaction |
56142
143+ ---
144+
57145## Error Responses
58146
59147### 401 Unauthorized
@@ -70,6 +158,8 @@ All protected routes return `401 Unauthorized` if no valid session exists.
70158}
71159```
72160
161+ ---
162+
73163## Authentication Flow
74164
751651 . ** Request Nonce** : ` POST /api/auth/nonce ` with ` { publicKey } `
@@ -79,6 +169,8 @@ All protected routes return `401 Unauthorized` if no valid session exists.
791695 . ** Protected Requests** : Cookie automatically sent with requests
801706 . ** Logout** : ` POST /api/auth/logout ` clears session
81171
172+ ---
173+
82174## Implementation Notes
83175
84176- Session stored as httpOnly cookie for security
@@ -87,6 +179,8 @@ All protected routes return `401 Unauthorized` if no valid session exists.
87179- Signature verification not yet implemented (TODO)
88180- Session storage in database not yet implemented (TODO)
89181
182+ ---
183+
90184## Contract Integration
91185
92186### Environment Variables
@@ -130,3 +224,6 @@ The following environment variables must be configured:
130224- ` 404 Not Found ` : Contract not deployed or split not configured
131225- ` 500 Internal Server Error ` : RPC connection error or contract read failure
132226- ` 400 Bad Request ` : Invalid parameters (calculate endpoint)
227+ ```
228+
229+ ---
0 commit comments