Skip to content

iamsuryansh/Shield

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ShieldAPI

AI-Powered Content Moderation API with Usage-Based Billing

A serverless, production-ready content moderation API built with FastAPI, React, AWS services, and Stripe. ShieldAPI provides enterprise-grade content safety detection using AWS Rekognition and Bedrock Claude, with transparent usage-based billing.


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   React Frontend β”‚  (Vite + Tailwind + Clerk Auth)
β”‚  (Developer Hub) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  API Gateway    β”‚  (Rate limiting, API key validation)
β”‚   (The Shield)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
    β”‚         β”‚
    β–Ό         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Lambda β”‚ β”‚ Step Functions β”‚  (Rekognition β†’ Bedrock)
β”‚        β”‚ β”‚  (The Brain)   β”‚
β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  DynamoDB   │────▢│  Stripe  β”‚
β”‚ (Single-Table) β”‚  β”‚ (Billing)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components

  1. Frontend (The Hub): React application for developer portal
  2. API Gateway (The Shield): Request throttling and API key validation
  3. Execution Layer (The Brain): Step Functions orchestrating AI models
  4. Billing Engine (The Pulse): Stripe Meters for real-time usage tracking
  5. Data Layer: DynamoDB with single-table design

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ and npm
  • Python 3.11+
  • AWS CLI configured with credentials
  • AWS CDK installed globally: npm install -g aws-cdk
  • Stripe account (for billing)
  • Clerk account (for authentication)

1. Clone and Configure

# Clone the repository
git clone <your-repo>
cd shield_ai

# Copy environment files
cp backend/.env.example backend/.env
cp frontend/.env.example frontend/.env

# Edit config.yaml with your settings
nano config.yaml

2. Set Up AWS Secrets

Store your sensitive credentials in AWS Secrets Manager:

# Stripe API key
aws secretsmanager create-secret \
  --name shieldapi/stripe/api_key \
  --secret-string "sk_test_YOUR_STRIPE_KEY"

# Clerk secret key
aws secretsmanager create-secret \
  --name shieldapi/clerk/secret_key \
  --secret-string "sk_test_YOUR_CLERK_KEY"

3. Deploy Infrastructure

cd infra

# Install dependencies
pip install -r requirements.txt

# Bootstrap CDK (first time only)
cdk bootstrap

# Deploy all stacks
cdk deploy --all

Note the outputs: API Gateway URL, DynamoDB table name, Usage Plan ID

4. Set Up Stripe Billing

# Create a billing meter for usage tracking
stripe meters create \
  --display-name "Moderation API Usage" \
  --event-name moderation_usage \
  --value-settings '{"event_payload_key": "value"}' \
  --default-aggregation '{"formula": "sum"}'

# Update config.yaml with the meter ID

5. Run Backend Locally

cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run FastAPI server
uvicorn app.main:app --reload

API will be available at http://localhost:8000

6. Run Frontend Locally

cd frontend

# Install dependencies
npm install

# Start dev server
npm run dev

Frontend will be available at http://localhost:3000


πŸ“‹ Configuration

All settings are centralized in config.yaml:

project:
  name: "ShieldAPI"
  stage: "prod"
  region: "us-east-1"

billing:
  provider: "stripe"
  meter_id: "mtr_12345"  # Update after creating Stripe meter

auth:
  provider: "clerk"
  jwt_issuer: "https://clerk.your-app.com"

limits:
  free_tier_monthly: 100
  burst_limit: 5
  rate_limit: 2

πŸ”‘ API Usage

1. Onboard and Get API Key

Sign up at the frontend (/dashboard) or call the onboard endpoint:

curl -X POST https://your-api-url/onboard \
  -H "Content-Type: application/json" \
  -d '{
    "clerk_user_id": "user_xyz",
    "email": "dev@example.com",
    "jwt": "eyJ..."
  }'

Response:

{
  "success": true,
  "api_key": "abcd1234...",
  "stripe_customer_id": "cus_xyz"
}

2. Moderate Content

curl -X POST https://your-api-url/moderate \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "image_url": "https://example.com/image.jpg",
    "idempotency_key": "unique-request-id"
  }'

Response:

{
  "success": true,
  "execution_arn": "arn:aws:states:...",
  "status": "PROCESSING"
}

πŸ—„οΈ Database Schema (DynamoDB)

Single-Table Design:

PK SK Attributes
USER#<id> PROFILE email, stripe_customer_id, created_at
USER#<id> API_KEY#<val> aws_key_id, status
USAGE#<id> DATE#<iso> total_calls, endpoint
USER#<id> IDEMPOTENCY#<key> response, timestamp

Global Secondary Indexes:

  • ApiKeyIndex: Query by api_key
  • StripeCustomerIndex: Query by stripe_customer_id

πŸ” Security Checklist

  • βœ… Secrets in AWS Secrets Manager (never hardcoded)
  • βœ… JWT validation with Clerk
  • βœ… Idempotency keys for duplicate prevention
  • βœ… Stripe webhook signature verification
  • βœ… API key rotation support
  • βœ… DynamoDB encryption at rest
  • βœ… Rate limiting at API Gateway level

πŸ§ͺ Testing

Test Onboarding

cd backend
python -m pytest tests/test_onboarding.py

Test Moderation Pipeline

# Start a moderation request
curl -X POST http://localhost:8000/moderate \
  -H "x-api-key: test-key" \
  -H "Content-Type: application/json" \
  -d '{"image_url": "https://example.com/test.jpg"}'

πŸ“Š Monitoring & Debugging

View CloudWatch Logs

# API Gateway logs
aws logs tail /aws/apigateway/ShieldAPI --follow

# Lambda logs
aws logs tail /aws/lambda/OnboardFunction --follow

# Step Functions execution history
aws stepfunctions list-executions \
  --state-machine-arn <your-state-machine-arn>

View DynamoDB Items

aws dynamodb scan --table-name shieldapi-users

Check Stripe Events

stripe events list --limit 10

🚒 Deployment to Production

Frontend Deployment (S3 + CloudFront)

cd frontend

# Build production bundle
npm run build

# Deploy to S3
aws s3 sync dist/ s3://shieldapi-frontend-prod --delete

# Invalidate CloudFront cache
aws cloudfront create-invalidation \
  --distribution-id <your-distribution-id> \
  --paths "/*"

Infrastructure Updates

cd infra

# Preview changes
cdk diff

# Deploy updates
cdk deploy --all

πŸ”„ Step Functions Pipeline

Moderation State Machine:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Rekognition     β”‚ (Detect moderation labels)
β”‚  Detection       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Choice β”‚ (High confidence violation?)
    β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
        β”‚
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
   β”‚          β”‚
   β–Ό          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Flag β”‚  β”‚ Bedrock β”‚ (Contextual analysis)
β”‚      β”‚  β”‚ Claude  β”‚
β””β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚ Format   β”‚
         β”‚ Response β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ’° Pricing Model

Free Tier: 100 requests/month (tracked via Stripe Meters)

Usage-Based:

  • Requests 1-100: Free
  • Requests 101-1,000: $0.01 each
  • Requests 1,000+: $0.005 each

Billing is handled automatically through Stripe with real-time metering.


πŸ“š Project Structure

shield_ai/
β”œβ”€β”€ config.yaml                 # Central configuration
β”œβ”€β”€ .github/
β”‚   └── copilot-instructions.md # Project guidelines
β”œβ”€β”€ infra/                      # AWS CDK infrastructure
β”‚   β”œβ”€β”€ app.py
β”‚   β”œβ”€β”€ stacks/
β”‚   β”‚   β”œβ”€β”€ api_stack.py
β”‚   β”‚   β”œβ”€β”€ database_stack.py
β”‚   β”‚   └── pipeline_stack.py
β”‚   └── utils/
β”‚       └── config_loader.py
β”œβ”€β”€ backend/                    # FastAPI application
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py
β”‚   β”‚   β”œβ”€β”€ config.py
β”‚   β”‚   └── services/
β”‚   β”‚       β”œβ”€β”€ auth.py
β”‚   β”‚       β”œβ”€β”€ billing.py
β”‚   β”‚       β”œβ”€β”€ database.py
β”‚   β”‚       └── stepfunctions.py
β”‚   └── lambdas/
β”‚       β”œβ”€β”€ onboard/
β”‚       β”œβ”€β”€ moderate/
β”‚       └── webhook/
└── frontend/                   # React application
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ pages/
    β”‚   β”‚   β”œβ”€β”€ Landing.jsx
    β”‚   β”‚   └── Dashboard.jsx
    β”‚   └── components/
    β”‚       β”œβ”€β”€ ApiKeyManager.jsx
    β”‚       β”œβ”€β”€ UsageChart.jsx
    β”‚       └── BillingPortal.jsx
    └── package.json

πŸ› οΈ Troubleshooting

Issue: CDK Deploy Fails

Solution: Ensure AWS credentials are configured and you've run cdk bootstrap

aws configure
cdk bootstrap aws://ACCOUNT-ID/REGION

Issue: API Key Not Working

Solution: Check API key is associated with usage plan:

aws apigateway get-usage-plan-keys --usage-plan-id <plan-id>

Issue: Stripe Billing Not Recording

Solution: Verify webhook endpoint is receiving events:

stripe listen --forward-to localhost:8000/webhook

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“„ License

MIT License - see LICENSE file for details


πŸ”— Resources


πŸ“§ Support

For issues or questions:


Built with ❀️ using FastAPI, React, and AWS

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors