Skip to content

Latest commit

Β 

History

History
298 lines (229 loc) Β· 7.88 KB

File metadata and controls

298 lines (229 loc) Β· 7.88 KB

πŸš€ PiPilot Search API - Ready to Launch!

βœ… What's Complete

1. Infrastructure (100% Done)

  • βœ… Cloudflare Worker deployed: https://pipilot-search-api.hanscadx8.workers.dev
  • βœ… KV namespaces created and configured
  • βœ… Global quota management (95k/day with graceful degradation)
  • βœ… Rate limiting (per-user and global)
  • βœ… 3 endpoints: /search, /extract, /smart-search
  • βœ… CLI tool for manual key management

2. Product Pages (100% Done)

  • βœ… Landing page (/api)

    • Hero with value prop
    • Live code examples
    • Pricing table (4 tiers)
    • Features showcase
    • FAQ section
  • βœ… Dashboard (/dashboard/api)

    • Subscription status
    • Usage quota display
    • API key management (generate, view, revoke)
    • Quick start guide
  • βœ… Checkout flow (/api/checkout)

    • Stripe Checkout integration
    • Subscription creation

3. Backend APIs (100% Done)

  • βœ… /api/keys/generate - Generate API keys (stored in KV)
  • βœ… /api/keys/list - List user's keys (from KV)
  • βœ… /api/keys/revoke - Revoke keys (updates KV)
  • βœ… /api/subscription/current - Get subscription (from KV)
  • βœ… /api/stripe/create-checkout - Stripe checkout session

4. Stripe Products (100% Done)

  • βœ… Starter: $29/mo (prod_U8KFiBzqU8sSFJ)
  • βœ… Pro: $149/mo (prod_U8KFSvIHon4TPZ)
  • βœ… Free tier (no Stripe product needed)
  • βœ… Enterprise (contact sales)

5. Navigation (100% Done)

  • βœ… Search API added to Products dropdown
  • βœ… Shows as "AI search for your apps"

πŸ”§ Setup Required (Before Launch)

1. Add Environment Variables

Add these to your .env.local or Vercel environment:

# Cloudflare (for KV access from Next.js API routes)
CLOUDFLARE_ACCOUNT_ID=your_cloudflare_account_id
CLOUDFLARE_API_TOKEN=your_cloudflare_api_token

# Stripe
STRIPE_SECRET_KEY=sk_live_xxx  # Use your Stripe secret key
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_xxx  # Get from Stripe dashboard

# App URL
NEXT_PUBLIC_APP_URL=https://pipilot.dev  # Or your current domain

To get Cloudflare Account ID:

  1. Go to Cloudflare dashboard
  2. Click on Workers & Pages
  3. Copy the Account ID from the right sidebar

πŸ“¦ KV Data Structure

Everything is stored in Cloudflare KV (no Supabase tables needed!):

// API Key
"{apiKey}": {
  id, name, tier, userId, userEmail,
  createdAt, totalRequests, lastUsedAt,
  revoked, revokedAt, rateLimit
}

// User's keys list
"user:{userId}:keys": [apiKey1, apiKey2, ...]

// User's subscription (created on upgrade)
"user:{userId}:subscription": {
  tier, status, stripeCustomerId,
  stripeSubscriptionId, periodEnd
}

// Global quota (already working)
"global:quota:{YYYY-MM-DD}": usageCount

// Rate limit (already working)
"ratelimit:{key}:{hour}": requestCount

πŸ”— User Flow

Free Tier Flow

  1. User visits /api
  2. Clicks "Get Started Free"
  3. Redirects to signup (if not logged in)
  4. After signup β†’ Redirects to /dashboard/api
  5. User generates API key
  6. Key stored in KV: user:{userId}:subscription = {tier: "free"}
  7. User copies key and starts using API

Paid Tier Flow

  1. User visits /api
  2. Clicks "Upgrade to Starter/Pro"
  3. Redirects to signup (if not logged in)
  4. Redirects to /api/checkout?plan=starter
  5. Creates Stripe checkout session
  6. User completes payment
  7. Stripe webhook creates subscription in KV
  8. User redirected to /dashboard/api?success=true
  9. Auto-generated API key ready
  10. User starts using API

🎯 Testing Checklist

Before Public Launch

  • Add environment variables to Vercel
  • Test free tier signup flow
  • Generate test API key
  • Verify API key works with Worker
  • Test paid tier checkout (use Stripe test mode first)
  • Verify Stripe webhook creates subscription in KV
  • Test quota tracking in dashboard
  • Test key revocation
  • Test rate limiting

Optional (Nice to Have)

  • Add Stripe webhook handler (/api/webhooks/stripe)
  • Add homepage announcement banner
  • Add Search API section to /docs
  • Send welcome email on first API key generation
  • Add usage analytics dashboard

🚨 Critical: Stripe Webhook

You need to create a Stripe webhook to handle subscription events:

1. Create Webhook Endpoint

File: app/api/webhooks/stripe/route.ts

import { NextRequest, NextResponse } from 'next/server'
import Stripe from 'stripe'

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: '2024-12-18.acacia',
})

export async function POST(request: NextRequest) {
  const body = await request.text()
  const signature = request.headers.get('stripe-signature')!

  let event: Stripe.Event

  try {
    event = stripe.webhooks.constructEvent(
      body,
      signature,
      process.env.STRIPE_WEBHOOK_SECRET!
    )
  } catch (err: any) {
    return NextResponse.json({ error: err.message }, { status: 400 })
  }

  // Handle events
  switch (event.type) {
    case 'checkout.session.completed': {
      const session = event.data.object as Stripe.Checkout.Session
      const userId = session.metadata?.user_id
      const tier = session.metadata?.tier

      if (userId && tier) {
        // Store subscription in KV
        const subscriptionData = {
          tier,
          status: 'active',
          stripeCustomerId: session.customer,
          stripeSubscriptionId: session.subscription,
          periodEnd: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(),
          createdAt: new Date().toISOString()
        }

        // Store in KV via Cloudflare API
        const kvUrl = `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/storage/kv/namespaces/e3b571cde10d48e38fdb107e0b9e2911/values/user:${userId}:subscription`

        await fetch(kvUrl, {
          method: 'PUT',
          headers: {
            'Authorization': `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(subscriptionData)
        })

        // Auto-generate API key for paid users
        // ... call /api/keys/generate internally
      }
      break
    }

    case 'customer.subscription.updated':
    case 'customer.subscription.deleted':
      // Update subscription status in KV
      break
  }

  return NextResponse.json({ received: true })
}

2. Register Webhook in Stripe

  1. Go to Stripe Dashboard β†’ Developers β†’ Webhooks
  2. Add endpoint: https://pipilot.dev/api/webhooks/stripe
  3. Select events:
    • checkout.session.completed
    • customer.subscription.updated
    • customer.subscription.deleted
  4. Copy webhook signing secret
  5. Add to .env.local: STRIPE_WEBHOOK_SECRET=whsec_xxx

πŸ“Š Revenue Projections

Based on your pricing (10x cheaper than competitors):

Conservative (3 months)

  • 100 users: 70 free, 20 starter, 8 pro, 2 enterprise
  • MRR: $2,772 ($33k/year)

Moderate (6 months)

  • 500 users: 350 free, 100 starter, 40 pro, 10 enterprise
  • MRR: $13,860 ($166k/year)

Optimistic (12 months)

  • 1,000 users: 700 free, 200 starter, 80 pro, 20 enterprise
  • MRR: $27,720 ($332k/year)

πŸŽ‰ Ready to Launch!

Once you:

  1. βœ… Add environment variables
  2. βœ… Create Stripe webhook
  3. βœ… Test the flows

You're ready to:

  • Announce on Twitter/X
  • Post on ProductHunt
  • Share on HackerNews
  • Email existing PiPilot users
  • Update homepage with banner

πŸ’‘ Quick Start for Users

# 1. Sign up at pipilot.dev/api
# 2. Generate API key in dashboard
# 3. Start using:

curl -X POST https://pipilot-search-api.hanscadx8.workers.dev/search \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "AI news", "maxResults": 5, "rerank": true}'

Built with ❀️ by Hans Ade - Pixelways Solutions Inc

Live API: https://pipilot-search-api.hanscadx8.workers.dev

Landing Page: https://pipilot.dev/api (once deployed)

Dashboard: https://pipilot.dev/dashboard/api (once deployed)