← back to Dear Bubbe Nextjs

CLOUDFLARE-SETUP.md

147 lines

# Cloudflare API Setup for Cache Purging

## Quick Setup (5 minutes)

Follow these steps to enable automatic cache purging:

### Step 1: Get Your Cloudflare API Token

1. **Log in to Cloudflare**
   - Go to: https://dash.cloudflare.com/login
   - Email: `Stev@designerwallcoverings.com`
   - Password: `*Gmailaccess911*`

2. **Navigate to API Tokens**
   - Click on your profile icon (top right)
   - Select "My Profile"
   - Go to "API Tokens" tab
   - Or visit directly: https://dash.cloudflare.com/profile/api-tokens

3. **Create a New API Token**
   - Click "Create Token" button
   - Choose "Create Custom Token" or use "Edit zone DNS" template

4. **Configure Token Permissions**
   - **Permissions:**
     - Zone → Cache Purge → Purge
     - Zone → Zone → Read
   - **Zone Resources:**
     - Include → Specific zone → `bubbe.ai`
   - **Client IP Address Filtering:** (optional)
     - Leave blank or add server IP: `45.61.58.125`
   - **TTL:** Start time and End time (optional)
     - You can leave this blank for no expiration

5. **Create and Copy Token**
   - Click "Continue to summary"
   - Click "Create Token"
   - **IMPORTANT:** Copy the token immediately (it's only shown once!)

6. **Save Token to Server**
   ```bash
   ssh root@45.61.58.125
   echo "YOUR_TOKEN_HERE" > /root/.cloudflare-token
   chmod 600 /root/.cloudflare-token
   ```

### Step 2: Test the Setup

```bash
cd /root/Projects/dear-bubbe-nextjs
./purge-cache.sh
```

You should see:
```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Cloudflare Cache Purge & Service Restart
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✓ Using API Token authentication
✓ Zone ID: xxxxxxxxxxxxxxxxxxxxxxxxxx
✓ Cache purged successfully
✓ Build completed successfully
✓ Service restarted successfully
✓ Service is online
✓ Website is responding (HTTP 200)
```

---

## Alternative: Global API Key (Less Secure)

If you prefer to use the Global API Key instead:

1. **Get Global API Key**
   - Go to: https://dash.cloudflare.com/profile/api-tokens
   - Scroll down to "API Keys" section
   - Click "View" next to "Global API Key"
   - Enter your password when prompted
   - Copy the API key

2. **Save to Server**
   ```bash
   ssh root@45.61.58.125
   echo "YOUR_API_KEY_HERE" > /root/.cloudflare-key
   chmod 600 /root/.cloudflare-key
   ```

**Note:** The Global API Key has full access to your entire Cloudflare account. The API Token approach is more secure as it limits permissions to only what's needed.

---

## Usage

Once set up, purge cache and restart with one command:

```bash
cd /root/Projects/dear-bubbe-nextjs
./purge-cache.sh
```

Or invoke the skill through Claude:
```
Use the purge-cache skill
```

---

## Troubleshooting

### "No Cloudflare credentials found"
- Make sure you saved the token/key to the correct file
- Check file exists: `ls -la /root/.cloudflare-*`
- Verify file has content: `cat /root/.cloudflare-token`

### "Could not fetch Zone ID"
- Verify your API credentials are correct
- Check token has correct permissions (Zone Read)
- Ensure domain name is spelled correctly

### "Failed to purge cache"
- Verify token has "Cache Purge" permission
- Check token isn't expired
- Verify you selected the correct zone (bubbe.ai)

### "Build failed"
- Check build logs: `tail -50 /tmp/bubbe-build.log`
- Try manual build: `cd /root/Projects/dear-bubbe-nextjs && npm run build`

---

## Security Notes

- API tokens are stored in `/root/` with 600 permissions (owner read/write only)
- Never commit these files to git
- Rotate tokens periodically for security
- Use API tokens (not Global API Key) for better security
- Add IP restrictions if accessing from fixed locations

---

## Additional Resources

- [Cloudflare API Documentation](https://developers.cloudflare.com/api/)
- [Create API Tokens](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/)
- [Cache Purge API](https://developers.cloudflare.com/api/operations/zone-purge)