|
| 1 | +# BottleCRM API |
| 2 | + |
| 3 | +Express.js API for BottleCRM with JWT authentication, Swagger documentation, and configurable request logging. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Google OAuth Authentication**: Secure Google Sign-In for mobile apps |
| 8 | +- **Multi-tenant**: Organization-based data isolation using existing Prisma schema |
| 9 | +- **Swagger Documentation**: Interactive API documentation at `/api-docs` |
| 10 | +- **Request Logging**: Configurable input/output HTTP request logging |
| 11 | +- **Security**: Helmet, CORS, rate limiting |
| 12 | +- **Organization Access Control**: Ensures users can only access their organization's data |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +1. The required environment variables are already added to your existing `.env` file. |
| 17 | + |
| 18 | +2. **Generate a secure JWT secret** (required for production): |
| 19 | +```bash |
| 20 | +# Using Node.js |
| 21 | +node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" |
| 22 | + |
| 23 | +# Using OpenSSL (if available) |
| 24 | +openssl rand -hex 32 |
| 25 | + |
| 26 | +# Using online generator (for development only) |
| 27 | +# Visit: https://generate-secret.vercel.app/32 |
| 28 | +``` |
| 29 | + |
| 30 | +3. Update your `.env` file with the generated secret: |
| 31 | +```env |
| 32 | +JWT_SECRET=your-generated-secret-key-here |
| 33 | +``` |
| 34 | + |
| 35 | +4. Start the API server: |
| 36 | +```bash |
| 37 | +# Development with auto-reload |
| 38 | +pnpm run api:dev |
| 39 | + |
| 40 | +# Production |
| 41 | +pnpm run api:start |
| 42 | +``` |
| 43 | + |
| 44 | +5. Visit Swagger documentation: |
| 45 | +``` |
| 46 | +http://localhost:3001/api-docs |
| 47 | +``` |
| 48 | + |
| 49 | +## Authentication |
| 50 | + |
| 51 | +1. **Google Login**: POST `/api/auth/google` |
| 52 | + - Request: `{ "idToken": "google-id-token-from-mobile-app" }` |
| 53 | + - Response: `{ "token": "jwt-token", "user": {...} }` |
| 54 | + |
| 55 | +2. **Use Token**: Include in Authorization header: |
| 56 | + ``` |
| 57 | + Authorization: Bearer <jwt-token> |
| 58 | + ``` |
| 59 | + |
| 60 | +3. **Select Organization**: Include organization ID in header: |
| 61 | + ``` |
| 62 | + X-Organization-ID: <organization-id> |
| 63 | + ``` |
| 64 | + |
| 65 | +## API Endpoints |
| 66 | + |
| 67 | +### Authentication |
| 68 | +- `POST /api/auth/google` - Google OAuth mobile login |
| 69 | +- `GET /api/auth/me` - Get current user profile |
| 70 | + |
| 71 | +### Leads |
| 72 | +- `GET /api/leads` - Get organization leads (paginated) |
| 73 | +- `GET /api/leads/:id` - Get lead by ID |
| 74 | +- `POST /api/leads` - Create new lead |
| 75 | + |
| 76 | +### Accounts |
| 77 | +- `GET /api/accounts` - Get organization accounts |
| 78 | +- `POST /api/accounts` - Create new account |
| 79 | + |
| 80 | +### Contacts |
| 81 | +- `GET /api/contacts` - Get organization contacts |
| 82 | +- `POST /api/contacts` - Create new contact |
| 83 | + |
| 84 | +### Opportunities |
| 85 | +- `GET /api/opportunities` - Get organization opportunities |
| 86 | +- `POST /api/opportunities` - Create new opportunity |
| 87 | + |
| 88 | +## Configuration |
| 89 | + |
| 90 | +### Environment Variables |
| 91 | + |
| 92 | +- `API_PORT`: Server port (default: 3001) |
| 93 | +- `JWT_SECRET`: Secret key for JWT tokens (required) - **Generate using the commands above** |
| 94 | +- `JWT_EXPIRES_IN`: Token expiration time (default: 24h) |
| 95 | +- `FRONTEND_URL`: Frontend URL for CORS (default: http://localhost:5173) |
| 96 | + |
| 97 | +### Logging Configuration |
| 98 | + |
| 99 | +- `LOG_LEVEL`: Logging level (info, debug, error) |
| 100 | +- `ENABLE_REQUEST_LOGGING`: Enable/disable request logging (true/false) |
| 101 | +- `LOG_REQUEST_BODY`: Log request bodies (true/false) |
| 102 | +- `LOG_RESPONSE_BODY`: Log response bodies (true/false) |
| 103 | + |
| 104 | +### Security Features |
| 105 | + |
| 106 | +- **Rate Limiting**: 100 requests per 15 minutes per IP |
| 107 | +- **Helmet**: Security headers |
| 108 | +- **CORS**: Cross-origin request handling |
| 109 | +- **JWT Validation**: Token verification on protected routes |
| 110 | +- **Organization Isolation**: Users can only access their organization's data |
| 111 | + |
| 112 | +## Data Access Control |
| 113 | + |
| 114 | +All API endpoints enforce organization-based access control: |
| 115 | + |
| 116 | +1. **Authentication Required**: All endpoints (except login) require valid JWT token |
| 117 | +2. **Organization Header**: Protected endpoints require `X-Organization-ID` header |
| 118 | +3. **Membership Validation**: User must be a member of the specified organization |
| 119 | +4. **Data Filtering**: All database queries are filtered by organization ID |
| 120 | + |
| 121 | +## Development |
| 122 | + |
| 123 | +The API uses the same Prisma schema as the main SvelteKit application, ensuring data consistency and leveraging existing: |
| 124 | + |
| 125 | +- Database models and relationships |
| 126 | +- Organization-based multi-tenancy |
| 127 | +- User role management (ADMIN/USER) |
| 128 | +- Super admin access (@micropyramid.com domain) |
| 129 | + |
| 130 | +## Testing with Swagger |
| 131 | + |
| 132 | +Access the interactive API documentation at `http://localhost:3001/api-docs` to: |
| 133 | + |
| 134 | +1. Test authentication endpoints |
| 135 | +2. Explore available endpoints |
| 136 | +3. Test API calls with different parameters |
| 137 | +4. View request/response schemas |
0 commit comments