A comprehensive Lightning-powered NIP-05 identity service with automatic database management, Nostr DM notifications, and flexible user management. Supports both Lightning payments and admin-only registration modes.
- π Lightning Payments: Integrated with LNbits for instant Bitcoin payments (optional)
- π NIP-05 Identity: Full NIP-05 identity verification support
- π Background Polling: Robust payment verification with webhook fallback
- π‘οΈ Admin Controls: Secure API for comprehensive user management
- π Health Monitoring: Built-in health checks with startup diagnostics
- ποΈ Database Support: SQLite by default, PostgreSQL ready with auto-migration
- π Nostr Sync: Automatic username sync from Nostr profiles (kind:0 events)
- π¬ Nostr DMs: Automated direct message notifications for user events
- π Whitelist.json: File-based user management with database sync
- βοΈ Flexible Modes: Lightning mode or admin-only mode
- π CORS Support: Configurable cross-origin resource sharing
- π API Documentation: Interactive Swagger documentation
- π§ Auto Database Management: Automatic schema updates and health checks
# Clone the repository
git clone https://github.com/Michilis/simple-nip5-api.git
cd simple-nip5-api
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Copy environment template
cp env.example .env
# Edit configuration
nano .envLightning Mode Configuration:
# Lightning Settings
LNBITS_ENABLED=true
LNBITS_API_KEY=your-lnbits-api-key
LNBITS_ENDPOINT=https://your-lnbits-instance.com
# Domain & Security
DOMAIN=yourdomain.com
ADMIN_API_KEY=your-secret-admin-key-here
WEBHOOK_URL=https://yourdomain.com/api/webhook/paid
# Nostr DM Notifications (optional)
NOSTR_DM_ENABLED=true
NOSTR_DM_PRIVATE_KEY=your-nostr-private-key-hex
NOSTR_DM_RELAYS=wss://relay.damus.io,wss://nostr.bitcoiner.social
# CORS (optional)
CORS_ENABLED=true
CORS_ORIGINS=*Admin-Only Mode Configuration:
# Lightning Settings
LNBITS_ENABLED=false
# Domain & Security
DOMAIN=yourdomain.com
ADMIN_API_KEY=your-secret-admin-key-here
# Optional Features
NOSTR_DM_ENABLED=false
CORS_ENABLED=true# Development mode
python run.py
# Production mode
uvicorn app.main:app --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
Once your application is running, visit /api-docs for comprehensive Swagger documentation:
http://localhost:8000/api-docs
| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
| Public Endpoints | |||
/.well-known/nostr.json |
GET | NIP-05 identity resolution | None |
/api/invoice |
POST | Create Lightning invoice | None |
/api/user/info |
POST | Get user information by npub/pubkey | None |
/api/webhook/paid |
POST | Payment webhook (LNbits) | None |
/health |
GET | Detailed system health check | None |
| Admin Endpoints | |||
/api/whitelist/add |
POST | Add user manually | API Key |
/api/whitelist/remove |
POST | Remove user | API Key |
/api/whitelist/users |
GET | List all users | API Key |
/api/whitelist/activate/{npub} |
POST | Activate user | API Key |
/api/whitelist/deactivate/{npub} |
POST | Deactivate user | API Key |
/api/whitelist/sync-usernames |
POST | Sync usernames from Nostr | API Key |
/api/whitelist/set-username |
POST | Set username manually | API Key |
/api/whitelist/remove-username |
POST | Remove manual username setting | API Key |
Many endpoints now support flexible input formats:
- npub format:
npub1abc123...(bech32 encoded) - hex pubkey:
abc123def456...(64-character hex string)
Supported endpoints:
/api/user/info-npubfield/api/whitelist/add-npubfield/api/whitelist/remove-npubfield/api/whitelist/activate/{npub}- path parameter/api/whitelist/deactivate/{npub}- path parameter
- Public Registration: Users can self-register by paying Lightning invoices
- Automatic Processing: Background tasks monitor and process payments
- Admin Override: Admins can still manually add/remove users
- Full Feature Set: All endpoints and functionality available
- DM Notifications: Automatic payment confirmations and updates
- Manual Registration: Only admins can add/remove users
- No Payment Processing: Lightning endpoints return 503 Service Unavailable
- Resource Efficient: No background invoice polling
- Pure NIP-05 Service: Focus on identity resolution without payments
- Whitelist Management: Use whitelist.json or API for user management
The application includes a comprehensive database management system:
- Automatic Migrations: Schema updates applied on startup
- Health Checks: 4-stage startup validation process
- Cross-Database Support: SQLite, PostgreSQL, MySQL compatible
- Error Recovery: Detailed diagnostics and repair guidance
- Backup Friendly: Safe migration with rollback support
- Database Initialization - Creates tables and runs migrations
- Schema Verification - Confirms all required columns exist
- Configuration Validation - Checks critical settings
- Whitelist Synchronization - Syncs whitelist.json if present
Check startup status at /health endpoint.
Create a whitelist.json file for bulk user management:
{
"users": [
{
"pubkey": "npub1abc123...",
"username": "alice",
"active": true,
"note": "Early supporter"
},
{
"pubkey": "def456789abcdef...",
"username": "bob",
"active": true,
"note": "Manual addition"
}
]
}- Startup Sync: Automatically syncs on application start
- Conflict Resolution: Whitelist.json takes precedence over database
- Username Protection: Whitelist users get manual username protection
- Status Tracking: View sync status in health endpoint
The system automatically syncs usernames from users' Nostr profiles:
- Background Task: Runs every 60 minutes (configurable)
- Profile Fetching: Queries Nostr relays for kind:0 (profile) events
- Name Extraction: Extracts the
namefield from profile metadata - Validation: Ensures the name is valid for NIP-05 usage
- Conflict Handling: Resolves username conflicts intelligently
- Rate Limiting: Max once per 24 hours per user
- Manual Override: Set usernames manually via API
- Sync Exclusion: Manually set usernames excluded from auto-sync
- Flexible Management: Enable/disable auto-sync per user
NOSTR_RELAYS=wss://relay.azzamo.net,wss://relay.damus.io,wss://primal.net
USERNAME_SYNC_ENABLED=true
USERNAME_SYNC_INTERVAL_MINUTES=60
USERNAME_SYNC_MAX_AGE_HOURS=24Send automatic DM notifications for user events:
- Payment Confirmed: When Lightning payment is received
- User Whitelisted: When manually added by admin
- User Removed: When removed from whitelist
- Subscription Expiry: Warnings and notifications
- Username Updates: When username changes
Customize messages in messages.json:
{
"payment_confirmed": {
"subject": "Payment Confirmed!",
"body": "Your payment has been confirmed! Welcome to {domain}..."
},
"user_whitelisted": {
"subject": "Welcome to {domain}!",
"body": "You have been added to our NIP-05 service..."
}
}NOSTR_DM_ENABLED=true
NOSTR_DM_PRIVATE_KEY=your-nostr-private-key-hex
NOSTR_DM_RELAYS=wss://relay.damus.io,wss://nostr.bitcoiner.social
NOSTR_DM_FROM_NAME=NIP-05 ServiceConfigure cross-origin resource sharing for web applications:
# Enable/disable CORS
CORS_ENABLED=true
# Allow all origins (development)
CORS_ORIGINS=*
# Specific origins (production)
CORS_ORIGINS=https://yourwebsite.com,https://app.yoursite.com
# Additional CORS settings
CORS_ALLOW_CREDENTIALS=true
CORS_ALLOW_METHODS=GET,POST,PUT,DELETE
CORS_ALLOW_HEADERS=*- User Requests Invoice: POST to
/api/invoicewith username and npub - Invoice Created: System creates LNbits invoice and returns payment request
- User Pays: User pays the Lightning invoice
- Payment Notification: LNbits sends webhook or system polls for payment
- User Activated: System activates user and includes in nostr.json
- DM Notification: User receives payment confirmation (if enabled)
- Username Sync: System periodically updates username from Nostr profile
| Variable | Description | Default |
|---|---|---|
DOMAIN |
Your domain name | "localhost" |
ADMIN_API_KEY |
Admin API authentication key | "your-secret-admin-key-here" |
DATABASE_URL |
Database connection string | "sqlite:///./nip05.db" |
| Variable | Description | Default |
|---|---|---|
LNBITS_ENABLED |
Enable Lightning payment functionality | true |
LNBITS_API_KEY |
LNbits API key | "" |
LNBITS_ENDPOINT |
LNbits instance URL | "https://demo.lnbits.com" |
NIP05_YEARLY_PRICE_SATS |
Yearly subscription price | 1000 |
NIP05_LIFETIME_PRICE_SATS |
Lifetime subscription price | 5000 |
INVOICE_EXPIRY_SECONDS |
Invoice expiration time | 1800 (30 min) |
WEBHOOK_URL |
Webhook callback URL | "http://localhost:8000/api/webhook/paid" |
| Variable | Description | Default |
|---|---|---|
USERNAME_SYNC_ENABLED |
Enable automatic username sync | true |
USERNAME_SYNC_INTERVAL_MINUTES |
Sync interval in minutes | 60 |
USERNAME_SYNC_MAX_AGE_HOURS |
Max hours between syncs per user | 24 |
NOSTR_RELAYS |
Comma-separated list of Nostr relays | "wss://relay.azzamo.net,..." |
| Variable | Description | Default |
|---|---|---|
NOSTR_DM_ENABLED |
Enable DM notifications | true |
NOSTR_DM_PRIVATE_KEY |
Nostr private key for sending DMs | "" |
NOSTR_DM_RELAYS |
Relays for sending DMs | "wss://relay.damus.io,..." |
NOSTR_DM_FROM_NAME |
Sender name for DMs | "NIP-05 Service" |
| Variable | Description | Default |
|---|---|---|
CORS_ENABLED |
Enable CORS middleware | true |
CORS_ORIGINS |
Allowed origins (* for all) | "*" |
CORS_ALLOW_CREDENTIALS |
Allow credentials | true |
CORS_ALLOW_METHODS |
Allowed HTTP methods | "*" |
CORS_ALLOW_HEADERS |
Allowed headers | "*" |
| Variable | Description | Default |
|---|---|---|
WHITELIST_FILE |
Whitelist JSON file path | "whitelist.json" |
MESSAGES_FILE |
DM messages template file | "messages.json" |
Production .env Example:
# Core Settings
DOMAIN=nip05.yourdomain.com
ADMIN_API_KEY=super-secret-admin-key-123
DATABASE_URL=postgresql://user:pass@localhost/nip05db
# Lightning Settings
LNBITS_ENABLED=true
LNBITS_API_KEY=your-production-lnbits-key
LNBITS_ENDPOINT=https://your-lnbits.com
WEBHOOK_URL=https://nip05.yourdomain.com/api/webhook/paid
# Nostr Settings
USERNAME_SYNC_ENABLED=true
NOSTR_DM_ENABLED=true
NOSTR_DM_PRIVATE_KEY=your-nostr-private-key
NOSTR_DM_RELAYS=wss://relay.damus.io,wss://nostr.bitcoiner.social
# Security
CORS_ENABLED=true
CORS_ORIGINS=https://yoursite.com,https://app.yoursite.comMIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request