|
1 | 1 | # SMS Service |
2 | 2 |
|
3 | | -NestJS microservice for sending verification codes, alerts, and time-sensitive text messages. |
| 3 | +A NestJS microservice for sending SMS verification codes, alerts, reminders, and other time-sensitive notifications. |
4 | 4 |
|
5 | 5 | ## Features |
6 | 6 |
|
7 | | -- SMS, message, and delivery receipt entities backed by TypeORM/PostgreSQL |
8 | | -- Provider abstraction with local console sending and Twilio HTTP support |
9 | | -- Secure OTP generation with hashed verification codes and expiry |
10 | | -- Handlebars message templates |
11 | | -- Scheduled message dispatch through `@nestjs/schedule` |
12 | | -- Delivery receipt webhook endpoint |
13 | | -- Phone validation, per-phone rate limiting, history, and analytics |
14 | | -- Docker and docker-compose configuration |
| 7 | +- Twilio, AWS SNS, and built-in mock provider support |
| 8 | +- Secure OTP generation and verification |
| 9 | +- Message templates with Handlebars rendering |
| 10 | +- Scheduled delivery with automatic retry backoff |
| 11 | +- Delivery receipts and webhook/manual confirmation endpoints |
| 12 | +- Phone number normalization and validation |
| 13 | +- Message history and analytics |
| 14 | +- Rate limiting per phone number and stricter OTP throttling |
15 | 15 |
|
16 | | -## Local Run |
| 16 | +## Quick Start |
17 | 17 |
|
18 | 18 | ```bash |
19 | 19 | cd microservices/sms-service |
20 | 20 | npm install |
| 21 | +cp .env.example .env |
21 | 22 | npm run start:dev |
22 | 23 | ``` |
23 | 24 |
|
24 | | -The default `SMS_PROVIDER=console` logs outbound SMS messages and marks them sent. Set `SMS_PROVIDER=twilio` with `TWILIO_ACCOUNT_SID`, `TWILIO_AUTH_TOKEN`, and `TWILIO_FROM_NUMBER` to send real SMS. |
| 25 | +The default configuration uses the `mock` provider, so the service can run locally without Twilio or AWS credentials. |
25 | 26 |
|
26 | | -## API |
| 27 | +## Key Environment Variables |
27 | 28 |
|
28 | | -- `GET /health` |
29 | | -- `POST /sms/send` |
30 | | -- `POST /sms/send-template` |
31 | | -- `POST /sms/otp` |
32 | | -- `POST /sms/otp/verify` |
33 | | -- `POST /sms/receipts` |
34 | | -- `GET /sms` |
35 | | -- `GET /sms/:id` |
36 | | -- `POST /sms/:id/cancel` |
37 | | -- `GET /sms-analytics` |
38 | | -- `GET /sms-templates` |
| 29 | +| Variable | Description | Default | |
| 30 | +|---|---|---| |
| 31 | +| `SERVICE_PORT` | HTTP port | `3007` | |
| 32 | +| `DB_TYPE` | `postgres` or `sqljs` | `postgres` | |
| 33 | +| `SMS_PROVIDER` | `mock`, `twilio`, or `sns` | `mock` | |
| 34 | +| `SMS_DEFAULT_COUNTRY` | Phone parsing fallback region | `US` | |
| 35 | +| `SMS_DISPATCH_INTERVAL_MS` | Poll interval for scheduled/retry messages | `5000` | |
| 36 | +| `SMS_RATE_LIMIT_MAX_PER_WINDOW` | Max SMS per phone per window | `20` | |
| 37 | +| `SMS_OTP_RATE_LIMIT_MAX_PER_WINDOW` | Max OTP sends per phone per window | `3` | |
| 38 | +| `SMS_OTP_EXPIRY_MINUTES` | OTP expiration | `10` | |
| 39 | +| `SMS_DEBUG_EXPOSE_CODES` | Include OTP codes in responses for local testing | `false` | |
39 | 40 |
|
40 | | -## Docker |
| 41 | +## Main Endpoints |
| 42 | + |
| 43 | +### Service |
| 44 | + |
| 45 | +- `GET /` - service info |
| 46 | +- `GET /health` - health check |
| 47 | + |
| 48 | +### SMS |
| 49 | + |
| 50 | +- `POST /sms/send` - send plain SMS |
| 51 | +- `POST /sms/send-templated` - send SMS from template |
| 52 | +- `GET /sms/history` - list message history |
| 53 | +- `GET /sms/stats` - aggregated delivery analytics |
| 54 | +- `POST /sms/:id/cancel` - cancel pending/scheduled SMS |
| 55 | +- `POST /sms/:id/retry` - retry failed SMS |
| 56 | + |
| 57 | +### Templates |
| 58 | + |
| 59 | +- `POST /templates` - create template |
| 60 | +- `GET /templates` - list templates |
| 61 | +- `GET /templates/:id` - fetch template |
| 62 | +- `GET /templates/name/:name` - fetch template by name |
| 63 | +- `PUT /templates/:id` - update template |
| 64 | +- `DELETE /templates/:id` - delete template |
| 65 | +- `POST /templates/render` - render a template payload |
| 66 | + |
| 67 | +### OTP |
| 68 | + |
| 69 | +- `POST /otp/send` - generate and send verification code |
| 70 | +- `POST /otp/verify` - verify a code |
| 71 | + |
| 72 | +### Receipts |
| 73 | + |
| 74 | +- `POST /receipts/confirm` - manually confirm/update delivery status |
| 75 | +- `POST /receipts/webhook` - provider webhook target |
| 76 | +- `GET /receipts/message/:messageId` - list receipt events for a message |
| 77 | + |
| 78 | +## Example Requests |
| 79 | + |
| 80 | +### Send an Alert |
41 | 81 |
|
42 | 82 | ```bash |
43 | | -cd microservices/sms-service |
44 | | -docker compose -f docker/docker-compose.yml up --build |
| 83 | +curl -X POST http://localhost:3007/sms/send \ |
| 84 | + -H "Content-Type: application/json" \ |
| 85 | + -d '{ |
| 86 | + "phoneNumber": "+15555550123", |
| 87 | + "body": "Your Quest account password was changed.", |
| 88 | + "type": "alert", |
| 89 | + "priority": "high" |
| 90 | + }' |
45 | 91 | ``` |
| 92 | + |
| 93 | +### Create a Template |
| 94 | + |
| 95 | +```bash |
| 96 | +curl -X POST http://localhost:3007/templates \ |
| 97 | + -H "Content-Type: application/json" \ |
| 98 | + -d '{ |
| 99 | + "name": "otp-verification", |
| 100 | + "body": "Your {{purpose}} code is {{code}}. It expires in {{expiryMinutes}} minutes.", |
| 101 | + "category": "otp" |
| 102 | + }' |
| 103 | +``` |
| 104 | + |
| 105 | +### Send OTP |
| 106 | + |
| 107 | +```bash |
| 108 | +curl -X POST http://localhost:3007/otp/send \ |
| 109 | + -H "Content-Type: application/json" \ |
| 110 | + -d '{ |
| 111 | + "phoneNumber": "+15555550123", |
| 112 | + "purpose": "login" |
| 113 | + }' |
| 114 | +``` |
| 115 | + |
| 116 | +## Testing |
| 117 | + |
| 118 | +```bash |
| 119 | +npm run build |
| 120 | +npm run test:e2e |
| 121 | +``` |
| 122 | + |
| 123 | +The e2e test suite runs against `sqljs` and the mock provider, so it does not require PostgreSQL or a real SMS account. |
0 commit comments