apis/
directory gives you a starting point with common patterns for querying your indexed blockchain data and serving it through HTTP endpoints.
Before deploying, you can run and test your API locally.
Navigate to the API directory
Set Up Environment Variable
.dev.vars
in the apis/
directory. Add the database connection string, which you can find on the App Page after deploying your app for the first time.Install Dependencies
Start the Development Server
http://localhost:8787
.apis/src/index.ts
is a TypeScript application that runs on Cloudflare Workers. It connects to your indexed database and exposes HTTP endpoints to query your data. Let’s understand how this works:
sim-idx
helper library, which wraps Hono and Drizzle to simplify setup:
sim-idx
handles database credentials for you in both local development and deployed environments, so no additional environment variables are required.
db.client(c)
inside a request handler to reuse the shared Drizzle client:
/api/pools
- Get recent Uniswap V3 pools/api/owner-changed
- Get recent owner changed events/api/pools/count
- Get total number of poolspool_created
table we created in our listener:
LIMIT 10
clause ensures we don’t return too much data at once. In a production environment, you might want to add pagination and filtering options.
OwnerChanged
event in your listener contract by following the “Triggering more functions and events” section of the Listener guide and then running:sim build
regenerates apis/src/db/schema/Listener.ts
with a new ownerChanged
table binding that we import below.owner_changed
table:
http://localhost:8787/api/pools
- Get recent poolshttp://localhost:8787/api/owner-changed
- Get recent owner changed eventshttp://localhost:8787/api/pools/count
- Get total pool count401 Unauthorized
error.
During local development, the authentication middleware automatically disables authentication checks when your API is running locally (i.e., when NODE_ENV
is not production
). This allows for a frictionless development experience without needing to manage API keys while testing your endpoints.
To generate a new API key, visit the Keys page and click the New button.
apis/src/index.ts
already includes the necessary authentication setup:
middlewares.authentication
is applied to all routes using the app.use("*", middlewares.authentication)
line. This ensures that every endpoint in your API requires authentication when deployed.
Authorization
header with every request.
Here’s an example using cURL:
<your-api-url>
with your deployment’s base URL and YOUR_SIM_IDX_APP_ENDPOINTS_API_KEY
with a valid Sim IDX App Endpoints API key.
main
branch to roll out a new production deployment.