AI Code Extension web Gateway

Case Study | AI SaaS Backend
A full-stack, enterprise-grade AI SaaS Backend built on WordPress. Engineered to power desktop IDE extensions with advanced billing, multi-provider routing, and hybrid auth.

Beyond a API Wrapper

Transforming WordPress into a headless SaaS provider.

While most AI plugins are designed for front-end website content generation, this Gateway handles the complex logistics of fractional token billing, hybrid authentication (Firebase + WP), and multi-provider failover routing. It allows frontend developers to focus purely on the client experience while the backend handles monetization, usage limits, and seamless integration between models like Claude, OpenAI, and Gemini.

Core Capabilities

Multi-Provider AI Orchestration

Intelligently routes inference requests between OpenAI, Anthropic (Claude), and Google Gemini with Automatic Failover to ensure zero downtime for edge users.

Credit Ledger & Pricing Engine

Universal currency system. Deducts credits with micro-cent precision based on Input/Output tokens, powered by a predictive algorithm to maintain high net margins.

Hybrid Authentication

Seamless passwordless social logins via Firebase (Google, Apple) mapped securely to native WordPress User IDs, combining modern auth with legacy DB stability.

Gamified Rewards Engine

Built-in growth loop. Users complete verifiable social/content tasks (e.g. referrals) and the admin approves them to instantly inject free API credits into their wallets.

Stripe & Billing Automation

Completely hands-off checkout via native shortcodes. Secure Webhooks listen for tier level purchases and automatically balance user ledgers.

Enterprise Security & Serverless

AES-256 encrypted API Keys. Supports Tavily AI Search (RAG), Model Context Protocol, and AWS Lambda integration to offload heavy handshakes.

Architectural Integrity

Built entirely with a modular, object-oriented approach for absolute reliability, security, and high-throughput endpoint responses bypassing standard WP meta bloat.

WordPress Backend

PHP 7.4+

Custom MySQL Schema

Firebase Auth

Stripe Webhooks

AES-256

REST API (/v2/)

Provider Manager

Standardizes connection protocols. If Anthropic goes down, seamlessly shifts traffic to OpenAI instantly.

Ledger & Pricing Analyzer

Decouples raw AI token costs from retail user Credits. Calculates fractional margins based on precise usage logs.

Relational DB Provisioning

Installs 7 custom relational MySQL tables on activation (`users`, `usage`, `transactions`, `tasks`, etc.) maximizing speed over `wp_postmeta`.

API Reference

Detailed REST API architecture powering the VS Code Extension. Built for performance, headless security, and precise cost tracking.

POST
/inference

Core Capabilities

Primary endpoint intelligently routing prompts between OpenAI, Gemini, and Claude. Natively supports 7 distinct developer IDE commands including Code Generation, Refactoring, Auto-Documentation (JSDoc), Unit Testing, and Terminal Error Debugging.

Request Payload Architecture
{
  "api_key": "aes_encrypted_key",
  "model": "auto",
  "messages": [
    {
      "role": "user",
      "content": "Generate a React component with pagination..."
    }
  ],
  "max_tokens": 800
}

Error Handling & Testing Scenarios

  • 401 Unauthorized: Invalid or revoked API key rejection.
  • 402 Payment Required: Insufficient wallet credits returned with precise required_credits delta.
  • 429 Too Many Requests: Automated rate-limiting with retry_after TTL.
Error Response Example
{
  "error": "Insufficient credits",
  "required_credits": 1.5,
  "available_credits": 0.14
}

The central AI engine. This single endpoint natively routes multi-LLM traffic across your preferred providers, natively powering 7 distinct IDE developer commands:

POST
/tavily

RAG Web Exploration

Built-in web search billed cleanly via the user’s single API key. Supports basic Search (0.1 cr), Deep Research (0.5 cr), Conversational Chat (0.3 cr), and JSON schema Data Extraction (0.4 cr).

{
  "endpoint": "extract",
  "query": "Extract tech company info",
  "options": {
    "extraction_type": "structured",
    "schema": {
      "company_name": "string",
      "founding_year": "number"
    }
  }
}

An integrated Retrieval-Augmented Generation context hub. It supports 5 completely distinct research modes. Costs are fractionally calculated and billed safely via the user’s universal API key.

POST
/usage-history

Paginated Token Audit Ledger

Extensive history pipeline offering pagination filtering, enabling precise token tracking and generating direct profit_usd metrics independently per request.

{
  "success": true,
  "total": 42,
  "items": [{
    "provider": "openai",
    "input_tokens": 600,
    "credits_deducted": 0.1234,
    "api_cost_usd": 0.001234,
    "profit_usd": 0.000456
  }]
}

An enterprise-grade ledger audit pipeline tracking the precise margin metric on every individual API call ever made. Allows the UI frontend to render paginated transaction history effortlessly.

Transforming WordPress into a Headless SaaS Infrastructure

Developing front-end applications (like VS Code extensions) is only half the battle. This project solves the backend infrastructure challenge. Rather than exposing raw, unmetered AI endpoints to end-users, this Gateway acts as a secure intermediary layer. It manages user authentication, precise financial billing, and intelligent AI failover routing, ensuring the application remains profitable, stable, and highly scalable.

Under The Hood

Custom REST Namespace

Operates entirely via /wp-json/aibuddy-code/v2/, bypassing slow WP-Admin AJAX calls for highly performant, sub-second latency.

Relational Database Schema

Provisions 7 custom MySQL tables (e.g., wp_aibuddy_gateway_usage) handling massive log volumes without utilizing slow WordPress meta-data.

Front-end Modularity

Employs 11 custom Shortcodes and deep Elementor Integration, allowing site owners to rapidly deploy dashboards without writing front-end code.

Predictive Pricing Engine & Margins

Because different AI models (like Claude Opus vs. Gemini) charge completely different internal rates based on token volume, rigid retail pricing quickly becomes unprofitable.

This system tracks every single token ingested and outputted. It then utilizes a predictive AI pricing algorithm to calculate the Raw Cost vs. Retail Value. If margins dip below optimal levels, the dashboard flags a “Low Rating”, ensuring continuous profitability.

Predictive Margin Algorithm Flagging Package Profitability

Gamified Rewards Engine

Predictive Margin Algorithm Flagging Package Profitability

To reduce Customer Acquisition Cost (CAC) and encourage organic marketing, a native growth loop was engineered into the core platform.

Users can earn free inference credits by completing assigned tasks, such as creating a promotional TikTok or referring colleagues. Submissions are pooled in the backend where an administrator can audit the proof (e.g., URL verification) and instantly inject the credit bounty into the user’s ledger.

Admin Verification Backend

Hybrid Auth & AES Encryption

Bridging modern authentication with traditional user management. The platform offloads initial social registration friction to Google Firebase (supporting Google, Apple, and Phone OTP).

Once authenticated via Firebase, the backend provisions a native WordPress user mapping. Crucially, the system generates custom `/v2/` API keys for IDE integration—which are symmetrically encrypted using AES-256 before being stored in the database, preventing unauthorized exposure.