Server-Side Integration

Target URL Verification

When users access your PayGate, Sigwei forwards paid requests to your target URL with authentication headers. You have two authentication modes to choose from.

Payment Headers

All proxied requests include these payment headers, regardless of authentication mode:

  • X-Payment: Base64-encoded payment authorization data (contains payer address, amount, nonce, signature, etc.)

  • X-Payment-Response: Base64-encoded settlement response from blockchain (contains transaction hash, network, success status)

These headers allow your target server to:

  • Verify the payment independently if desired

  • Track which wallet paid for the request

  • Link requests to on-chain transactions

  • Implement custom payment verification logic

When headerAuthMode is set to "hmac" (default), Sigwei adds an HMAC signature for secure authentication:

Signature Computation: HMAC-SHA256(X-Payment + "|" + X-Payment-Response, secret)

The signature proves the request came from Sigwei and includes valid payment data.

Plaintext Mode (Simple)

When headerAuthMode is set to "plaintext", Sigwei adds a simple secret header:

Why Authentication Matters

Security Issue: x-payment headers can be recreated from blockchain transaction data. Anyone could theoretically access protected PayGates by reconstructing payment headers from public blockchain transactions.

Solution: Enable requireAuth: true on PayGates. This ensures only the original payer (verified by wallet signature) can access content, even if someone else copies the payment header.

Why Sigwei Secret is Crucial

The Problem: Without verification, anyone could bypass your PayGate by calling your API directly, avoiding payment entirely.

The Solution: Sigwei Secret ensures requests come from legitimate paying users via Sigwei's proxy:

  • You set the secret when creating the PayGate or generate via API

  • Every paid request includes authentication headers

  • Your backend rejects requests without valid authentication

  • Direct API access is blocked, payments are enforced

Backend Implementation

Python Example

Managing Secrets

Get or Regenerate Secret

Request Timeouts & Size Limits

Sigwei enforces timeout and response size limits to prevent abuse and manage infrastructure costs.

Timeout Configuration

Default: 10 minutes (600 seconds)

The proxy timeout determines how long Sigwei will wait for your target URL to respond. This generous default supports:

  • AI endpoints (OpenAI, Claude, etc.)

  • Long-running computations

  • Large data processing

When requests timeout:

  • Client receives 502 Bad Gateway

  • Metrics are still captured for analytics

  • Credits are consumed (payment was made, timeout is server-side)

Best Practices:

  • Design APIs to respond within 10 minutes when possible

  • Use async patterns for longer operations (webhook callbacks)

  • Return processing IDs immediately, fetch results later

  • Consider chunking large responses

Response Size Limits

Default: 10MB (10,485,760 bytes)

Sigwei blocks responses exceeding this limit to prevent egress bandwidth abuse on hosting infrastructure.

When size limit exceeded:

  • Request blocked with 502 Bad Gateway

  • Warning logged with actual size and limit

  • Metrics captured showing blocked request

  • Credits are consumed (payment was made, size limit is protective)

Handling Large Responses:

For responses larger than 10MB, use File PayGates instead:

  • Upload files to Sigwei's storage (R2)

  • Users get presigned download URLs

  • No bandwidth cost to Sigwei (direct R2 → user)

  • Better user experience for large files

Example - Wrong way (proxy large data):

Example - Right way (use file PayGate):

API Design Recommendations

For API Providers:

  1. Paginate large datasets - Return data in chunks

  2. Stream processing - Use streaming responses where possible

  3. Compression - gzip/brotli compress responses

  4. Async patterns - Return job IDs for long operations

Example - Pagination:

Security Best Practices

  • Store secrets securely: Use environment variables or key management systems

  • Validate every request: Check authentication headers on all protected endpoints

  • Use HMAC mode: More secure than plaintext for production environments

  • Rotate secrets regularly: Update PayGate secrets periodically via API

  • Log access attempts: Monitor for unauthorized patterns and failed authentications

  • Rate limiting: Implement rate limiting on your target URLs to prevent abuse

  • Response size awareness: Design APIs to stay under 10MB or use file PayGates for larger content

Last updated