Create an AI-powered chat app that can answer questions about blockchain data using Dune’s Sim APIs and OpenAI’s function calling feature.
The Simchat interface we'll build - a conversational assistant for blockchain data
0xd8da6bf26964af9d7eed9e03e53415d37aa96045
DYw8jCTfwHNRJhhmFcbXvVDTqWMEVFBX6ZKUmG5CNSKK
on SolanaCreate Project Directory
Install Dependencies
Configure Environment Variables
.env
file in your project root:.env
file to version control. Add it to .gitignore
to keep your API keys secure.Add Starter Code
server.js
file will handle our backend Express server and API logic, while chat.html
contains our frontend chat interface.Populate the server.js
with this basic Express code:chat.html
:Verify Project Structure
node server.js
in the terminal to start the server.
Visit http://localhost:3001
to see the newly scaffolded chat app.
Our newly created chat front-end UI is ready.
.env
file contains the correct OPENAI_API_KEY
and SIM_API_KEY
.
Check your terminal for any error messages from server.js
.SYSTEM_PROMPT
variable to your server.js
file:
/chat
endpoint will receive POST requests from our frontend chat interface, process them through the LLM, and return responses to display in the chat:
node server.js
again and visit http://localhost:3001
.
You’ll have a working chat interface powered by OpenAI’s gpt-4o-mini
model with a custom system prompt, but it won’t be able to fetch realtime blockchain data yet.
The chat is now working with OpenAI responses, but not yet fetching blockchain data
functions
array to your server.js
file:
get_token_balances
, we need to:
URLSearchParams
for complex query strings, multiple path parameters, and endpoints with no parameters.
callFunction
that can handle both regular objects and URLSearchParams
:
API_CONFIGS
pattern while properly handling all the different parameter types and patterns used by the various Sim API endpoints.
The apiCall
function can handle both URLSearchParams
objects (for complex queries) and regular objects (for simple query parameters).
/chat
endpoint with this version that includes function calling support: