WordPress has a reputation as a blogging platform. FastAPI has a reputation as a Python microservice framework. Neither reputation tells the full story — and together, they make a surprisingly capable stack for building AI-powered web applications.
This post covers how I built a production-style integration between a WordPress frontend and a Python FastAPI backend, including how requests are secured, how the two systems communicate, and why I’d choose this combination again.
You can see it running in my AI Integration Demo and the Healthcare Plan RAG Demo.
Why This Combination?
Most AI integrations I see go one of two routes: a bespoke React/Next.js frontend talking directly to an API, or a WordPress plugin that bundles everything together. Both have tradeoffs.
The React approach gives you flexibility but means building and maintaining a full frontend stack. The plugin approach is fast but opaque — you’re at the mercy of someone else’s architecture and update cycle.
WordPress as a frontend with a decoupled Python backend hits a different target:
- WordPress handles content management, authentication, forms, caching, and the UI — things it’s genuinely good at
- FastAPI handles AI logic, data processing, and anything that benefits from Python’s ecosystem (vector search, ML libraries, async I/O)
- The two systems are loosely coupled — either can change independently
The Architecture
[ WordPress Frontend ]
- Renders the UI (shortcode or block)
- Handles form submission via JavaScript
- Proxies requests to FastAPI through PHP
|
▼
[ WordPress PHP Proxy ]
- Validates nonce
- Enforces rate limiting
- Injects private credentials
- Forwards request to FastAPI
|
▼
[ FastAPI Backend (Python) ]
- Validates auth key
- Runs AI / RAG logic
- Returns structured JSON
|
▼
[ WordPress returns response to browser ]
The browser never communicates with FastAPI directly. WordPress acts as a secure intermediary — more on why that matters in The Right Way to Integrate AI into WordPress.
The FastAPI Backend
FastAPI is a natural fit for this role. It’s fast (async by default), self-documenting (automatic OpenAPI docs), and has excellent support for the Python libraries you’d use in an AI backend — Pydantic, SQLAlchemy, pgvector, LangChain, and so on.
A minimal endpoint for the WordPress integration looks like this:
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
app = FastAPI()
class QuestionRequest(BaseModel):
question: str
@app.post("/ask")
async def ask(
request: QuestionRequest,
x_demo_key: str = Header(...)
):
if x_demo_key != settings.DEMO_KEY:
raise HTTPException(status_code=401, detail="Unauthorized")
# Run RAG pipeline
answer, citations = await run_rag_pipeline(request.question)
return {
"answer": answer,
"citations": citations
}
The X-Demo-Key header is injected by the WordPress PHP proxy — it’s never in the browser. FastAPI validates it on every request before running any logic.
The WordPress Side
On the WordPress side, a custom shortcode renders the UI and a PHP AJAX handler manages the proxy:
// Register the shortcode
add_shortcode('mrobbieb_ai_demo', 'render_ai_demo');
function render_ai_demo() {
wp_enqueue_script('mrobbieb-demo', get_stylesheet_directory_uri() . '/demo.js', ['jquery'], null, true);
wp_localize_script('mrobbieb-demo', 'mrobbiebDemo', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('mrobbieb_ai_nonce'),
]);
return '';
}
// AJAX handler — this is the proxy
add_action('wp_ajax_nopriv_mrobbieb_ai_query', 'handle_ai_query');
function handle_ai_query() {
check_ajax_referer('mrobbieb_ai_nonce', 'nonce');
// rate limiting, validation, wp_remote_post to FastAPI...
// (see full implementation in the proxy pattern post)
}
The shortcode outputs a container div. The JavaScript sends requests to admin-ajax.php, not to FastAPI. WordPress handles everything in between.
Deploying FastAPI Alongside WordPress
The two services run independently. In my setup:
- WordPress runs on a standard managed WordPress host
- FastAPI runs on a separate VPS or cloud instance (a small DigitalOcean droplet or Railway deployment works fine for demos)
- FastAPI is not publicly exposed — it’s behind a private URL or firewall rule, accessible only to the WordPress server’s IP
For production, you’d run FastAPI behind a reverse proxy (nginx or Caddy), use HTTPS end-to-end, and lock down the FastAPI port to only accept traffic from the WordPress server.
What This Stack Gets You
- Full Python ecosystem for AI work — vector DBs, async I/O, ML libraries, structured outputs
- WordPress content management — pages, users, forms, media, caching — without rebuilding any of it
- Clean separation of concerns — UI and content in WordPress, logic and data in FastAPI
- Independent deployability — update the AI backend without touching the WordPress site and vice versa
- Automatic API docs — FastAPI generates OpenAPI documentation at
/docs, useful for debugging and collaboration
When to Use This Pattern
This combination is a good fit when:
- You want to add AI to an existing WordPress site without rebuilding the frontend
- Your AI logic is complex enough to justify a dedicated Python service (RAG pipelines, multi-step agents, vector search)
- You need the backend to evolve independently of the UI
- Security matters — you want credentials and logic fully server-side
It’s less appropriate if your AI integration is simple enough for a WordPress plugin, or if you’re starting from scratch and would rather use a unified stack.
The full source for the energy-exchange project that uses this pattern is on GitHub. Questions or want to talk through your own setup? Get in touch.
Building something like this?
Get a free 30-minute architecture review or a written AI readiness audit — no commitment.

Martin Baker — Solutions Architect specializing in AI, RAG systems, and WordPress engineering. 15+ years building systems that hold up under real business pressure.