Sim IDX provides a complete serverless API development environment with built-in database connectivity, automatic scaling, and edge deployment. Whether you’re building simple data endpoints or advanced features, the platform handles infrastructure concerns so you can focus on your business logic.
This guide walks you through the complete API development lifecycle on the platform.
Your API runs on Cloudflare Workers with the Hono framework. Anything you can do in Hono will also work here. Your data is stored in a Neon Postgres accessed through Cloudflare Hyperdrive. Requests execute at the edge close to users, and database connections are pooled automatically, so you don’t have to manage servers or connection limits.
The setup scales with your traffic, but there are sensible platform limits. If you anticipate sustained very high volume, please contact us so we can make sure everything runs smoothly.
Building APIs on Sim IDX follows a streamlined workflow: develop locally, write endpoints to query your indexed data, and deploy with a simple git push.
The boilerplate API in the 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
Create a file named .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
Your API is now running locally at http://localhost:8787
.
The boilerplate API in 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:
The API uses the sim-idx
helper library, which wraps Hono and Drizzle to simplify setup:
Hono handles HTTP requests and routing, while Drizzle provides a type-safe way to query your PostgreSQL database.
sim-idx
handles database credentials for you in both local development and deployed environments, so no additional environment variables are required.
Create the web application with a single call:
Instead of managing your own connection pool, call db.client(c)
inside a request handler to reuse the shared Drizzle client:
Let’s build three endpoints to serve our indexed data:
/api/pools
- Get recent Uniswap V3 pools/api/owner-changed
- Get recent owner changed events/api/pools/count
- Get total number of poolsLet’s create our first endpoint to fetch the 10 most recent Uniswap V3 pools. This endpoint will query the pool_created
table we created in our listener:
This endpoint uses a simple SQL query to fetch the most recent pools. The 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.
Before continuing, make sure you’ve added support for the OwnerChanged
event in your listener contract by following the “Triggering more functions and events” section of the Listener guide and then running:
Running sim build
regenerates apis/src/db/schema/Listener.ts
with a new ownerChanged
table binding that we import below.
Now let’s add an endpoint to fetch the 10 most recent owner changed events. This will query the owner_changed
table:
Finally, let’s add an endpoint to get the total number of pools. This will be useful for pagination and analytics:
This endpoint uses an aggregate query to efficiently count pools without fetching every row.
After adding all three endpoints, you can test them locally:
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 countSim IDX provides built-in authentication middleware that integrates seamlessly with your app and the platform.
When deployed to production, the authentication middleware requires a valid Sim IDX App Endpoints API key to be passed with each request. Sim’s infrastructure validates the key and, if successful, allows the request to proceed. Unauthenticated requests will be rejected with a 401 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.
Your API will need a Sim IDX App Endpoints API key to access your authenticated endpoints. They can generate a new key from the Sim dashboard.
To generate a new API key, visit the Keys page and click the New button.
When creating the key, its purpose should be set to Sim IDX App Endpoints. This key must be kept secure and should not be exposed in client-side code.
The authentication middleware is enabled by default in your API. When you create a new Sim IDX app, the boilerplate code in apis/src/index.ts
already includes the necessary authentication setup:
The 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.
Once your API is deployed, you must include their Sim IDX App Endpoints API key in the Authorization
header with every request.
Here’s an example using cURL:
Replace <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.
Once your repository is connected, shipping updates is as simple as pushing code:
main
branch to roll out a new production deployment.Sim IDX automatically builds your Cloudflare Worker and updates the deployment status in the dashboard. No CLI command is required.