This workspace demonstrates two standard OAuth integration patterns for the Hypery.
Best for: SPAs, mobile apps, quick prototypes, real-time chat
User Browser → Chat Frontend → Core Hypery API
↑ (OAuth + PKCE)
- No
client_secretneeded - Code verifier generated client-side
- Secure for public clients (browsers, mobile)
See: chat/
Key file: chat/src/lib/oauth.ts
// Direct API call from frontend
const response = await fetch(`${AUTH_URL}/api/oauth/token`, {
body: JSON.stringify({
grant_type: 'authorization_code',
code,
code_verifier: verifier, // PKCE verifier
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
// NO client_secret - public client
})
});- Lower latency (no middleware hop)
- Simpler architecture
- Lower cost (no backend processing)
- Industry standard for AI APIs (OpenRouter, Replicate, etc.)
- Limited backend control
- Access token visible in browser (mitigated by short expiry)
- Building end-user applications
- Real-time chat/streaming apps
- Cost optimization is important
- Low latency is critical
Best for: Enterprise apps, third-party integrations, apps needing custom logic
User Browser → Imagine Frontend → Imagine Backend → Core Hypery API
↑ (stores client_secret)
client_secretstored server-side- Frontend only gets short-lived tokens
- Refresh tokens never exposed to browser
See: imagine/
Key files:
- Frontend:
imagine/src/lib/oauth.ts - Backend:
imagine/src/app/api/auth/token/route.ts
// Frontend calls own backend
const response = await fetch('/api/auth/token', {
body: JSON.stringify({
grant_type: 'authorization_code',
code,
code_verifier: codeVerifier
})
});
// Backend forwards to core with client_secret
export async function POST(request: NextRequest) {
const { code, code_verifier } = await request.json();
return fetch(`${CORE_APP_URL}/api/oauth/token`, {
body: JSON.stringify({
grant_type: 'authorization_code',
code,
code_verifier,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET, // Securely stored
redirect_uri: REDIRECT_URI
})
});
}- Client secret never exposed
- Can add rate limiting, caching, business logic
- Better monitoring and control
- Refresh tokens stored server-side
- Standard for enterprise OAuth
- Extra network hop (higher latency)
- More infrastructure to maintain
- Higher costs (backend processing)
- Building third-party integrations
- Enterprise/B2B customers
- Need custom backend logic (rate limiting, analytics)
- Maximum security required
- Managing multiple AI providers in backend
The Hypery core supports both patterns simultaneously:
Accepts:
- Public Client (PKCE) -
code_verifierpresent,client_secretoptional - Confidential Client -
client_secretpresent
// Schema validation (src/app/api/oauth/token/route.ts)
{
grant_type: 'authorization_code',
code: string,
redirect_uri: string,
client_id: string,
client_secret?: string, // Optional for public clients
code_verifier?: string, // Required for PKCE
}cd chat
npm install
npm run dev
# Visit http://localhost:3002cd imagine
npm install
npm run dev
# Visit http://localhost:3007| Feature | Frontend (chat) | Backend (imagine) |
|---|---|---|
| Latency | Lower (direct) | Higher (extra hop) |
| Cost | Lower | Higher (backend processing) |
| Security | PKCE | Client Secret |
| Complexity | Simple | Moderate |
| Backend Logic | Limited | Full control |
| Token Storage | Browser | Server |
| Best For | End users | Enterprise |
| Industry Examples | OpenRouter, Replicate | Stripe, Twilio |
- Uses PKCE to prevent authorization code interception
- Access tokens are short-lived (recommended: 15-60 min)
- Refresh tokens can be stored in
httpOnlycookies or not used - Suitable for trusted first-party apps
- Client secret never exposed to browser
- Refresh tokens stored securely server-side
- Can implement additional security layers (IP allowlisting, etc.)
- Required for third-party OAuth apps
- OAuth 2.0 RFC 6749: https://tools.ietf.org/html/rfc6749
- PKCE (RFC 7636): https://tools.ietf.org/html/rfc7636
- OAuth 2.0 for Browser-Based Apps: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps
Choose Pattern 1 (Frontend) if:
- Building your own apps
- Cost and latency matter
- Real-time features needed
- Following modern AI API patterns
Choose Pattern 2 (Backend) if:
- Third-party integration
- Enterprise customers
- Need custom middleware logic
- Maximum security required
- Managing secrets for multiple services
Most developers should start with Pattern 1 and only add Pattern 2 when specific requirements demand it.