This implementation adds a comprehensive API system to CodeNearby with token-based pricing, user tiers, and AI-powered developer search capabilities.
- Script:
scripts/migrate-users-for-api.js - Status: Successfully updated 448 users
- New Fields Added:
tier: User tier (free/premium)tokenBalance: Daily and purchased token trackingusage: Daily and total usage statisticsapiKeyCount: Number of API keys createdbilling: Purchase history and preferencesfeatures: Feature access based on tierverification: Account verification status
- Library:
lib/user-tiers.ts - Features:
- FREE tier: 1,000 daily tokens, 1 API key
- PREMIUM tier: 2,000 daily tokens, 10 API keys
- Token packages with bonus tokens
- Currency support (USD/INR)
- Daily token reset functionality
POST /api/v1/developers- AI-powered developer searchPOST /api/v1/profile- GitHub profile analysisPOST /api/v1/repositories- Repository search (Premium)GET /api/v1/users/tier- User tier and token information
POST /api/v1/auth/keys- Create API keysGET /api/v1/auth/keys- List user's API keysDELETE /api/v1/auth/keys- Delete API keysPOST /api/v1/billing/buy-tokens- Purchase token packagesGET /api/v1/users/api-key-permission- Check API key creation limits
- API Dashboard:
components/token-api-dashboard.tsx - Token Store:
components/token-upgrade-page.tsx - API Documentation:
app/api-docs/page.tsx - Currency Toggle:
components/ui/currency-toggle.tsx - Currency Hook:
hooks/use-currency.tsx
{
// Existing fields...
tier: "free" | "premium",
tokenBalance: {
daily: number,
purchased: number,
total: number
},
usage: {
today: {
tokens: number,
requests: number,
date: string // YYYY-MM-DD
},
total: {
tokens: number,
requests: number
}
},
apiKeyCount: number,
maxApiKeys: number,
billing: {
currency: "USD" | "INR",
totalSpent: number,
purchases: Array<{
packageId: string,
tokens: number,
amount: number,
currency: string,
date: Date,
transactionId?: string
}>
},
lastTokenReset: Date,
features: {
developerSearch: boolean,
profileAnalysis: boolean,
repositorySearch: boolean,
analytics: boolean,
prioritySupport: boolean
},
verification: {
email: boolean,
phone: boolean,
github: boolean
}
}{
_id: ObjectId,
userId: string,
name: string,
keyHash: string, // SHA-256 hash
keyPreview: string, // First 8 + last 4 chars
tier: string,
isActive: boolean,
createdAt: Date,
lastUsed: Date | null
}- BASIC: $9 USD / ₹49 INR - 5,000 tokens
- STANDARD: $25 USD / ₹149 INR - 17,000 tokens (15k + 2k bonus)
- PRO: $79 USD / ₹499 INR - 60,000 tokens (50k + 10k bonus)
- ENTERPRISE: $199 USD / ₹1,499 INR - 200,000 tokens (150k + 50k bonus)
- Developer Search: 200-800 tokens (avg: 400)
- Profile Analysis: 300-2,000 tokens (avg: 1,000)
- Repository Search: 150-1,200 tokens (avg: 600)
- User Tier Info: 0 tokens (free)
All API requests require an API key in the header:
x-api-key: your_api_key_here
API keys are:
- SHA-256 hashed for security
- Tied to user accounts and tiers
- Rate limited based on tier
- Trackable for usage analytics
node scripts/migrate-users-for-api.jsnpm run devcurl -X POST "http://localhost:3000/api/v1/auth/keys" \
-H "Content-Type: application/json" \
-H "Cookie: next-auth.session-token=YOUR_SESSION" \
-d '{"name": "Test API Key"}'curl -X POST "http://localhost:3000/api/v1/developers" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "Find React developers in New York"}'curl -X GET "http://localhost:3000/api/v1/users/tier" \
-H "x-api-key: YOUR_API_KEY"app/
├── api/
│ └── v1/
│ ├── auth/
│ │ └── keys/route.ts
│ ├── billing/
│ │ └── buy-tokens/route.ts
│ ├── developers/route.ts
│ ├── profile/route.ts
│ ├── repositories/route.ts
│ └── users/
│ ├── tier/route.ts
│ └── api-key-permission/route.ts
├── api-dashboard/page.tsx
├── api-docs/page.tsx
└── upgrade/page.tsx
components/
├── token-api-dashboard.tsx
├── token-upgrade-page.tsx
├── token-store-page.tsx
└── ui/
└── currency-toggle.tsx
hooks/
└── use-currency.tsx
lib/
├── user-tiers.ts
└── ai.ts
scripts/
└── migrate-users-for-api.js
- Payment Integration: Replace mock payment with real payment processors (Stripe, Razorpay)
- Rate Limiting: Implement request rate limiting based on tiers
- Analytics Dashboard: Build comprehensive usage analytics
- Webhooks: Add webhook support for real-time integrations
- Mobile SDKs: Create SDKs for mobile app integration
- GraphQL: Consider GraphQL API endpoints
- Enterprise Features: SSO, custom integrations, dedicated support
- API keys are SHA-256 hashed
- Session-based authentication for management endpoints
- Input validation on all endpoints
- MongoDB injection prevention
- Rate limiting (to be implemented)
- Token balance validation before API execution
- Database indexes on key fields
- Redis caching for AI responses (30 minutes)
- Efficient token consumption logic
- Optimized MongoDB queries
- Background token reset jobs
- Request/response logging
- Token usage tracking
- Error monitoring
- Performance metrics
- User tier analytics
- Payment transaction logs