$npx -y skills add berabuddies/Semia --skill sparkbtcbot-proxy-deployDeploy a serverless Spark Bitcoin L2 proxy on Vercel with spending limits, auth, and Redis logging. Use when user wants to set up a new proxy, configure env vars, deploy to Vercel, or manage the proxy infrastructure.
| 1 | # Deploy sparkbtcbot-proxy |
| 2 | |
| 3 | You are an expert in deploying and managing the sparkbtcbot-proxy — a serverless middleware that wraps the Spark Bitcoin L2 SDK behind authenticated REST endpoints on Vercel. |
| 4 | |
| 5 | ## What This Proxy Does |
| 6 | |
| 7 | Gives AI agents scoped wallet access without exposing the mnemonic: |
| 8 | - Role-based token auth (`admin` for full access, `invoice` for read + create invoices only) |
| 9 | - Token management via API — create, list, revoke without redeploying |
| 10 | - Per-transaction and daily spending caps |
| 11 | - Activity logging to Redis |
| 12 | - Lazy detection of paid Lightning invoices |
| 13 | |
| 14 | ## What You Need |
| 15 | |
| 16 | **Ask the user for these upfront:** |
| 17 | |
| 18 | - Vercel account (free Hobby tier works) |
| 19 | - Upstash account email and API key (from https://console.upstash.com/account/api) — OR existing `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` if they already have a database |
| 20 | - BIP39 mnemonic for the Spark wallet (or generate one in step 3) |
| 21 | - Node.js 20+ |
| 22 | |
| 23 | **Generated during setup (don't ask for these):** |
| 24 | |
| 25 | - `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` — created by the Upstash management API in step 2 |
| 26 | - `API_AUTH_TOKEN` — generated in step 4 |
| 27 | |
| 28 | ## Step-by-Step Deployment |
| 29 | |
| 30 | ### 1. Clone and install |
| 31 | |
| 32 | ```bash |
| 33 | git clone https://github.com/echennells/sparkbtcbot-proxy.git |
| 34 | cd sparkbtcbot-proxy |
| 35 | npm install |
| 36 | ``` |
| 37 | |
| 38 | ### 2. Create Upstash Redis |
| 39 | |
| 40 | If the user already has `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`, skip to step 3. |
| 41 | |
| 42 | Otherwise, create a database via the Upstash API. The user needs their Upstash email and API key from https://console.upstash.com/account/api: |
| 43 | |
| 44 | ```bash |
| 45 | curl -X POST "https://api.upstash.com/v2/redis/database" \ |
| 46 | -u "UPSTASH_EMAIL:UPSTASH_API_KEY" \ |
| 47 | -H "Content-Type: application/json" \ |
| 48 | -d '{"name": "sparkbtcbot-proxy", "region": "global", "primary_region": "us-east-1"}' |
| 49 | ``` |
| 50 | |
| 51 | **Note:** Regional database creation is deprecated. You must use `"region": "global"` with a `"primary_region"` field. The Upstash docs may not reflect this yet. |
| 52 | |
| 53 | The response includes `rest_url` and `rest_token` — save these for step 5. |
| 54 | |
| 55 | ### 3. Generate a wallet mnemonic (if needed) |
| 56 | |
| 57 | `SparkWallet.initialize()` returns `{ mnemonic, wallet }` when called without a mnemonic. One-liner: |
| 58 | |
| 59 | ```bash |
| 60 | node -e "import('@buildonspark/spark-sdk').then(({SparkWallet}) => SparkWallet.initialize({mnemonicOrSeed: null, options: {network: 'MAINNET'}}).then(r => { console.log(r.mnemonic); r.wallet.cleanupConnections() }))" |
| 61 | ``` |
| 62 | |
| 63 | Save the 12-word mnemonic securely — it controls all funds in the wallet. There is no `getMnemonic()` method; you can only retrieve the mnemonic at initialization time. |
| 64 | |
| 65 | Or use any BIP39 mnemonic generator. 12 or 24 words. |
| 66 | |
| 67 | ### 4. Generate an API auth token |
| 68 | |
| 69 | ```bash |
| 70 | openssl rand -base64 30 |
| 71 | ``` |
| 72 | |
| 73 | ### 5. Deploy to Vercel |
| 74 | |
| 75 | ```bash |
| 76 | npx vercel --prod |
| 77 | ``` |
| 78 | |
| 79 | When prompted, accept the defaults. Then set environment variables. All 7 are required: |
| 80 | |
| 81 | | Variable | Description | Example | |
| 82 | |----------|-------------|---------| |
| 83 | | `SPARK_MNEMONIC` | 12-word BIP39 mnemonic | `fence connect trigger ...` | |
| 84 | | `SPARK_NETWORK` | Spark network | `MAINNET` | |
| 85 | | `API_AUTH_TOKEN` | Admin fallback bearer token | output of step 4 | |
| 86 | | `UPSTASH_REDIS_REST_URL` | Redis REST endpoint | `https://xxx.upstash.io` | |
| 87 | | `UPSTASH_REDIS_REST_TOKEN` | Redis auth token | from step 2 | |
| 88 | | `MAX_TRANSACTION_SATS` | Per-transaction spending cap | `10000` | |
| 89 | | `DAILY_BUDGET_SATS` | Daily spending cap (resets midnight UTC) | `100000` | |
| 90 | |
| 91 | **Important:** Do NOT use `vercel env add` with heredoc/`<<<` input — it appends newlines that break the Spark SDK. Either use the Vercel dashboard or the REST API: |
| 92 | |
| 93 | ```bash |
| 94 | curl -X POST "https://api.vercel.com/v10/projects/<PROJECT_ID>/env?teamId=<TEAM_ID>" \ |
| 95 | -H "Authorization: Bearer <VERCEL_TOKEN>" \ |
| 96 | -H "Content-Type: application/json" \ |
| 97 | -d '{"type":"encrypted","key":"SPARK_MNEMONIC","value":"your mnemonic here","target":["production","preview","development"]}' |
| 98 | ``` |
| 99 | |
| 100 | Redeploy after setting env vars: |
| 101 | |
| 102 | ```bash |
| 103 | npx vercel --prod |
| 104 | ``` |
| 105 | |
| 106 | ### 6. Test |
| 107 | |
| 108 | ```bash |
| 109 | curl -H "Authorization: Bearer <your-token>" https://<your-deployment>.vercel.app/api/balance |
| 110 | ``` |
| 111 | |
| 112 | Should return `{"success":true,"data":{"balance":"0","tokenBalances":{}}}`. |
| 113 | |
| 114 | ### 7. Create scoped tokens (optional) |
| 115 | |
| 116 | Use the admin token to create limited tokens for agents: |
| 117 | |
| 118 | ```bash |
| 119 | curl -X POST -H "Authorization: Bearer <admin-token>" \ |
| 120 | -H "Content-Type: application/json" \ |
| 121 | -d '{"role": "invoice", "label": "my-agent"}' \ |
| 122 | https://<your-deployment>.vercel.app/api/tokens |
| 123 | ``` |
| 124 | |
| 125 | The response includes the full token string — save it, it's only shown once. See the **Token Roles** section below for details. |
| 126 | |
| 127 | ## API Routes |
| 128 | |
| 129 | | Method | Route | Description | |
| 130 | |------ |