This document is the developer reference for the HTTP client that connects the frontend to the Heliobond backend REST API:
src/lib/apiClient.ts— the typed/v1client.src/lib/api.ts— app-facing functions with demo fallbacks.src/app/api/backend/[...path]/route.ts— the server-side proxy that keeps the API key out of the browser.
Every function falls back to bundled fixture data when no backend is configured, so the app works out of the box without a running backend.
- Environment configuration
- Authentication
- Response types
- Endpoints
- Error handling
- Demo / fixture fallback
- Usage examples
| Variable | Required | Example | Purpose |
|---|---|---|---|
HELIOBOND_API_URL |
No | http://localhost:3001 |
Backend base URL, read server-side only. Falls back to NEXT_PUBLIC_API_URL when unset. |
HELIOBOND_API_KEY |
No | hb_live_… |
API key sent as X-API-Key. Server-side only — never expose it through a NEXT_PUBLIC_* variable. |
NEXT_PUBLIC_API_URL |
No | http://localhost:3001 |
Public base URL, used for the auth endpoints and as a fallback for HELIOBOND_API_URL. |
Copy .env.example to .env.local, then set HELIOBOND_API_URL and
HELIOBOND_API_KEY to point at your local or staging backend before running
bun run dev.
Leaving both API variables unset or empty (and leaving HELIOBOND_API_KEY empty)
uses bundled demo fixtures.
Every /v1 backend route requires an API key on the X-API-Key header. A
secret cannot live in a NEXT_PUBLIC_* variable — Next.js inlines those into
the client bundle, so anyone using the app could read the key and exhaust the
rate limit.
The browser therefore never talks to the backend directly. It calls the same-origin route handler, which attaches the key on the server:
browser ──▶ /api/backend/v1/projects (Next.js route handler)
│ adds X-API-Key server-side
▼
HELIOBOND_API_URL/v1/projects (backend)
Server components skip the proxy and call the backend directly, still with the key attached server-side.
The proxy forwards a fixed allowlist of read-only endpoints and rejects anything else without contacting the backend:
| Allowed upstream path | Used by |
|---|---|
/v1/projects |
getProjects, Explore |
/v1/projects/:id |
getProject |
/v1/projects/:id/history |
getPriceHistory |
/v1/portfolio/:address |
fetchBackendPortfolio |
/v1/forecast/:id |
forecasts |
All types are exported from src/lib/api.ts and can be imported directly.
Defined in src/data.ts. Core bond project record: id, name, yield, term,
rating, funded status, and pool-related fields.
Defined in src/data/projectDetails.ts. Extended record with oracle score
history, funding timeline, price/yield history, and creator attribution.
missingSections? names sections the source could not supply (for example a
backend-only project); the detail screen renders an explicit
"information unavailable" state for those instead of inventing content.
interface ProjectWithDetail {
project: Project
detail: ProjectDetail
}Returned by getProject.
interface BackendProject {
id: number
credit_quality: number // Credit Quality score, 0–100
green_impact: number // Green Impact score, 0–100
power_output_kw: number
efficiency_pct: number
forest_density_pct: number
ndvi_score: number
timestamp: number // Unix milliseconds
}The shape the backend returns. mapBackendProject
converts it into the app's Project, keeping names and funding copy from the
local project so a live backend only replaces the scores.
interface PaginatedProjectsResponse {
projects: Project[]
total: number // Total matching records (not just this page)
page: number // 1-indexed current page
pageSize: number // Items per page
hasMore: boolean // Whether another page exists
}Returned by getProjectsPaginated.
interface PricePoint {
date: string // ISO 8601 date, e.g. "2025-03-14"
price: number // Bond price in USDC
yield?: number // Yield at that date (percentage)
}Returned (as an array) by getPriceHistory.
async function getProjects(): Promise<Project[]>Fetches all bond projects.
Backend endpoint: GET /v1/projects?cursor=0&limit=100
Returns: Array of Project objects.
Throws: ApiError on network failures, HTTP errors, or timeouts (after 8 seconds).
Demo fallback: Returns all projects from src/data.ts via
selectProjects(). On-chain priority: If a project registry contract is
configured, data is read from the Stellar blockchain first.
Example:
import { getProjects } from '@/lib/api'
const projects = await getProjects()
// [{ id: 1, name: 'Solaris Alpha', yield: 6.5, ... }, ...]async function getProjectsPaginated(
page?: number, // default: 1
pageSize?: number, // default: 12
): Promise<PaginatedProjectsResponse>Fetches a paginated slice of bond projects. Use this for the Explore screen initial load — it reduces time-to-interactive from 3–5 s down to sub-second by deferring off-screen projects.
Backend endpoint: GET /v1/projects?cursor={(page-1)*pageSize}&limit={pageSize}
The backend paginates with a cursor, so the 1-indexed page number is translated
into an offset. hasMore is derived from whether the response carried a cursor
for the next page.
Parameters:
| Param | Type | Default | Description |
|---|---|---|---|
page |
number |
1 |
1-indexed page number |
pageSize |
number |
12 |
Items per page |
Returns: PaginatedProjectsResponse
Throws: ApiError on network failures, HTTP errors, or timeouts (after 8 seconds).
Demo fallback: Slices selectProjects() with the same pagination math.
On-chain priority: If a project registry contract is configured, data is
read from the Stellar blockchain first.
Example:
import { getProjectsPaginated } from '@/lib/api'
// First page
const page1 = await getProjectsPaginated(1, 12)
// { projects: [...], total: 34, page: 1, pageSize: 12, hasMore: true }
// Next page
const page2 = await getProjectsPaginated(2, 12)
// { projects: [...], total: 34, page: 2, pageSize: 12, hasMore: true }async function getProject(id: number): Promise<ProjectWithDetail | null>Fetches a single project with its full detail record.
Backend endpoint: GET /v1/projects/:id
Parameters:
| Param | Type | Description |
|---|---|---|
id |
number |
Must be a positive integer (id >= 1). Non-integer, negative, zero, or NaN ids return null immediately without a network call. |
Returns: ProjectWithDetail, or null when the backend has no such project
(a 404 resolves to null rather than raising). For a backend-only ID with no
bundled fixture, createBackendProjectDetail() supplies a complete detail
record with empty collections and missingSections; the detail screen shows an
explicit missing-information state instead of crashing on detail.creator or
detail.scoreHistory.
Throws: ApiError on network failures, HTTP errors, or timeouts (after 8 seconds).
Only a confirmed miss is null — a transient failure rejects so the page can
route to its error/retry UI instead of a not-found page.
Demo fallback: Looks up selectProjectById(id) and selectProjectDetail(id)
from fixture data. Returns null if either is missing. On-chain priority:
If a project registry contract is configured, project and detail data are read
from the Stellar blockchain first, with a verifiedMetadata status ('verified' | 'mismatch' | 'unverified')
indicating cryptographic on-chain hash verification of the off-chain metadata.
Example:
import { getProject } from '@/lib/api'
const result = await getProject(1)
if (result) {
const { project, detail } = result
console.log(project.name, detail.scoreHistory.credit)
}
// Invalid ids return null immediately — no request fired:
await getProject(-1) // null
await getProject(NaN) // null
await getProject(1.5) // nullThere is no client method for this, and that is deliberate. The backend never
implemented POST /investments, and a Stellar deposit transaction is the source
of truth for an investment — recording one server-side as well would create a
second, reconcilable copy that can disagree with the chain. Use the vault
helpers in src/wallet/vault.ts to submit the deposit.
function mapBackendProject(raw: BackendProject, fallback?: Project): ProjectConverts a backend row into the app's Project. The backend reports scores and
telemetry; names, locations and funding copy stay from the local project, so
turning on a live backend replaces the scores without blanking out the screen.
verifiedMetadata is false for backend data, since it is not verified
on-chain.
async function getScoreHistory(projectId: number): Promise<ScoreObservation[]>Fetches a project's score history as dated observations, oldest first.
Backend endpoint: GET /v1/projects/:projectId/history
The backend reports dated credit-quality and green-impact snapshots, not share
prices or yield observations. historyToScorePoints
maps those snapshots to { date, credit, green }; no price or yield is
synthesised. Those fields are reserved for data that actually measures them.
getPriceHistory / historyToPricePoints remain as deprecated aliases that
return the same score-only series.
Parameters:
| Param | Type | Description |
|---|---|---|
projectId |
number |
Project ID |
Returns: Array of ScoreObservation objects sorted in ascending
chronological order (oldest first), so a consumer can plot green impact over
time.
Throws: ApiError on network failures, HTTP errors, or timeouts (after 8 seconds).
Demo fallback: Generates a deterministic 30-day mock score series using
projectId as a seed so the sparkline shape is consistent per project across
renders.
Example:
import { getScoreHistory } from '@/lib/api'
const history = await getScoreHistory(2)
// [
// { date: '2025-08-06', credit: 74, green: 88 },
// { date: '2025-08-07', credit: 76, green: 89 },
// ...
// ]async function fetchBackendPortfolio(address: string): Promise<BackendPortfolio>Fetches the on-chain vault position and indexed event history for an address.
Backend endpoint: GET /v1/portfolio/{address}
Returns: BackendPortfolio — shares, USDC value, claimable yield, share of
pool, total deposited, and the address's vault events.
The Soroban vault remains the source of truth for balances; this endpoint is the backend's indexed view of the same data and is only used when a backend is configured.
async function biometricLogin(username?: string): Promise<boolean>Triggers a WebAuthn biometric prompt (Face ID / Touch ID) and delegates to the canonical challenge-response flow in webauthn.ts.
Client contract: Requests options from /webauthn/login/begin, presents them to navigator.credentials.get, and posts the assertion to /webauthn/login/complete. Completion must return HTTP success with { "verified": true }; HTTP success alone is insufficient. Requests use same-origin cookies and disable caching.
Backend dependency: This repository does not implement these endpoints. A backend must issue expiring, single-use challenges, verify assertions against stored public keys and the expected origin/RP ID, and establish an authenticated session before returning verification success. The client boolean is UI feedback, not server authorization.
Returns: true if the biometric assertion was successfully verified by the server, false if:
- The username is missing or blank (there is no shared default identity)
- WebAuthn is not supported (
window.PublicKeyCredentialis absent) - The user cancelled or failed the prompt
- Server challenge issuance or assertion verification failed (logged as a warning)
Environment: Only works in a browser context with a registered authenticator. Always returns false in SSR (window === undefined).
Example:
import { biometricLogin } from '@/lib/api'
const ok = await biometricLogin('user@example.com')
if (ok) {
// proceed with authenticated session
} else {
// show fallback (password, wallet sign-in, etc.)
}The API client reads project data from three sources in the following priority order:
- On-chain registry — If
isRegistryConfigured()returnstrue(when a Stellar ProjectRegistry contract is deployed and configured), project data is read directly from the blockchain via Stellar RPC. This is the highest- trust path and bypasses HTTP entirely. - Demo fixtures — If no backend URL or API key is configured, all functions
return deterministic mock data from
src/data.tsandsrc/data/projectDetails.ts. No HTTP requests are made. A "Demo data" badge appears in the UI whenshouldShowDemoBadge()returnstrue. - HTTP backend — If
HELIOBOND_API_URLorNEXT_PUBLIC_API_URLis set, the client calls the configured backend. Failures throwApiError— they are not silently swallowed.
Network failures, HTTP errors (non-2xx status), and timeouts all throw ApiError:
import { ApiError } from '@/lib/api'
try {
const projects = await getProjects()
} catch (err) {
if (err instanceof ApiError) {
console.error('API failed:', err.status, err.code, err.message)
// err.status: HTTP status (404, 503, etc.) if applicable
// err.code: Machine-readable code (e.g., "HTTP_503", "rpc-timeout")
// err.message: Human-readable message
}
}ApiError is defined in src/lib/error.ts and includes optional status,
code, and cause fields for debugging and error reporting.
All HTTP calls are wrapped in an 8-second timeout (API_TIMEOUT_MS). If a
request does not complete within 8 seconds, it is aborted and an error is
reported to telemetry with { kind: 'rpc-timeout', context: { target: 'api' } }.
The timeout error is then thrown to the caller.
// After 8 seconds, throws:
// Error: "timed out after 8000ms"
const projects = await getProjects()import { getProjects, ApiError } from '@/lib/api'
async function loadExploreScreen() {
try {
const projects = await getProjects()
// success — render projects
} catch (err) {
if (err instanceof ApiError) {
// show user-facing error: "Could not load projects. Please try again."
// log to error reporting: err.status, err.code, err.message
} else {
// unexpected error — rethrow or log
throw err
}
}
}For mapping backend error codes to user-facing strings, see
src/lib/errorMessages.ts and
ERROR_CODES.md.
When no backend URL or API key is configured, the app runs in demo mode. In this mode:
- No HTTP requests are made — every API function returns deterministic fixture data immediately.
- Fixture data comes from:
src/data.ts— pool summary, projects list, investor position, activity feedsrc/data/projectDetails.ts— per-project oracle history, creator info, funding timeline, price historysrc/state/selectors.ts— flat accessor functions over the above
shouldShowDemoBadge()returnstrue, and the UI displays a "Demo data" badge so users know the data is not live.
This means the full click-through works without a backend, including the Explore, Project Detail, and Deposit screens.
When a backend is configured, demo mode is off. HTTP failures
(timeouts, 5xx errors, network errors) throw ApiError and do not fall
back to fixtures. The caller must handle the error and show appropriate UI
(loading state, retry button, error message).
import { getProjectsPaginated } from '@/lib/api'
// Initial load — first 12 projects only
const { projects, hasMore, total } = await getProjectsPaginated(1, 12)
// Infinite scroll — fetch the next page when the sentinel enters the viewport
if (hasMore) {
const next = await getProjectsPaginated(2, 12)
}// src/app/project/[id]/page.tsx
import { notFound } from 'next/navigation'
import { getProject } from '@/lib/api'
import { ProjectDetailClient } from '@/app/project/[id]/ProjectDetailClient'
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id: rawId } = await params
const id = Number(rawId)
const data = await getProject(id)
if (!data) notFound()
return <ProjectDetailClient id={id} initialData={data} />
}Investments happen on-chain through submitDeposit; the backend has no
investment endpoint to call. The helper builds, signs, submits, and confirms
the transaction, and resolves to its transaction hash.
import { submitDeposit } from '@/wallet/vault'
async function createInvestment(
amount: number,
address: string,
sign: (xdr: string) => Promise<string>,
signal?: AbortSignal,
): Promise<string> {
return submitDeposit(amount, address, sign, signal)
}import { getPriceHistory } from '@/lib/api'
import { PriceHistoryChart } from '@/components'
const history = await getPriceHistory(projectId)
return <PriceHistoryChart data={history} />