Add Alchemy RPC To Any Project using Cursor
Learn how to add a server-safe Alchemy JSON RPC endpoint to any project using Cursor
This guide helps you add a server safe Alchemy JSON RPC endpoint into almost any repo using a single Cursor prompt. Your Alchemy URL stays in .env.local
on the server. The client never sees it.
What the one shot prompt sets up
- A JSON RPC proxy that forwards requests to Alchemy
- Multi network selection via a small
x-chain
header - A generic
ALCHEMY_API_URL
plus optional per chain overrides - Smart scaffolding based on your project type:
- Next.js App Router →
app/api/rpc/route.ts
(Edge) - Next.js Pages Router →
pages/api/rpc.ts
- Non Next.js → a small Node server at
server/rpc-proxy.mjs
with an npm script
- Next.js App Router →
- Environment files:
- Writes
.env.example
- Creates
.env.local
from.env.example
with a terminal command if it does not already exist - Ensures
.env.local
is in.gitignore
- Writes
- Optional helper and self test:
- Next.js helper
lib/rpc.ts
- Dev self test page for App Router projects at
/rpc-selftest
- Next.js helper
Prerequisites
- Cursor is open at your project root
- You can run a local dev server for your framework
- You have an Alchemy API Key ready to paste into
.env.local
- Optional for smoother automation in Cursor:
- Enable Auto run for the terminal
- Add a small command allowlist like
npm run dev
,node
,mkdir
,touch
What is the x-chain
header
The x-chain
header tells your proxy which network to use for a given request. Your app adds this header when it calls /api/rpc
. The proxy reads it and selects the matching Alchemy URL from your server env. The header is not sent to Alchemy.
Supported values out of the box
If you omit the header, the proxy assumes eth-mainnet
.
Resolution flow
- Read
x-chain
from the request. Default toeth-mainnet
if missing. - Try the chain specific env var.
- If not set, fall back to
ALCHEMY_API_URL
. - If neither exists, return a clear 500 with guidance.
Env file keys you will set
After the prompt runs it will create .env.example
and .env.local
(if missing). Replace <KEY>
with your real key:
Use only ALCHEMY_API_URL
if you want a single default network. Add per chain URLs when you want to route by x-chain
.
How to run
- Open Cursor at your project root
- Paste the one shot prompt from the next section into Cursor chat
- Let Cursor write files and run the terminal command that creates
.env.local
- Open
.env.local
and replace<KEY>
with your real Alchemy key(s) - Start your dev server and verify:
- Next.js App Router: open
http://localhost:3000/rpc-selftest
- Next.js Pages Router: use the curl commands the prompt writes to
docs/RPC-TEST.md
- Non Next.js: run
npm run rpc-proxy
and POST tohttp://localhost:8787/rpc
- Next.js App Router: open
How to call the proxy
Send JSON RPC 2.0 to /api/rpc
in Next.js, or to http://localhost:8787/rpc
in the fallback Node server. Choose the network with x-chain
. If you omit it, the proxy uses the generic ALCHEMY_API_URL
.
curl example
fetch example
One shot Cursor prompt
Paste everything below into Cursor chat from your project root.
Troubleshooting
-
500 Missing upstream
AddALCHEMY_API_URL
or a chain specific URL to.env.local
. Restart your server. -
400 Invalid JSON body
Ensure your request body is valid JSON and follows JSON RPC 2.0:{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}
-
502 Upstream returned non JSON
Check your Alchemy URL and key. Verify witheth_chainId
.
Quick verification checklist
.env.example
and.env.local
both exist- One of
ALCHEMY_API_URL
or a chain specific URL is set in.env.local
- App Router projects open
http://localhost:3000/rpc-selftest
and show block numbers for two chains - Pages Router projects can run the curl commands in
docs/RPC-TEST.md
without errors