Browser applications
A ParticleDB SDK for connecting directly from the browser, with per-user tokens and row-level access rules, is in development and not yet available. Today, connect from your backend with the published Node.js SDK, @particledb/sdk (npm install @particledb/sdk), and have the browser call your backend as shown below.
Put your application backend between the browser and ParticleDB. Authenticate the application user there, enforce access rules and use bound parameters for values supplied by users.
Read data from the browser
Section titled “Read data from the browser”This example calls an application route, not the database directly:
const response = await fetch('/api/tasks');if (!response.ok) throw new Error(`Request failed: ${response.status}`);const tasks = await response.json();console.log(tasks);Query from your backend
Section titled “Query from your backend”A Node.js backend can also use the native @particledb/sdk package on the database port, 7173. For a minimal Node.js example with the compatibility path, install pg and create the tasks table from the quickstart. Set DATABASE_URL in the backend environment using your deployment’s SQL-client address and TLS settings.
npm install pgSave this as server.mjs:
import http from 'node:http';import pg from 'pg';
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is required');const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const server = http.createServer(async (request, response) => { if (request.method !== 'GET' || request.url !== '/api/tasks') { response.writeHead(404).end(); return; } try { const result = await pool.query('SELECT id, title, status FROM tasks ORDER BY id'); response.writeHead(200, { 'Content-Type': 'application/json' }); response.end(JSON.stringify(result.rows)); } catch (error) { console.error('Task query failed', error.code); response.writeHead(503, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: 'Tasks are temporarily unavailable' })); }});server.listen(3000, '127.0.0.1');Run node server.mjs and request http://localhost:3000/api/tasks. Serve your frontend and this route under the same application origin, or configure your application’s reverse proxy to route /api/ to the backend.
This minimal example has no application login. Add authentication and account scoping before making it available to users. Scope every query to the authenticated account; do not trust an account identifier chosen by the browser.
Handle user input
Section titled “Handle user input”Use your client’s parameter API for filters and writes. Do not accept arbitrary SQL from the browser. See the TypeScript and Node.js guide for parameterized queries and transactions.
Direct HTTP access
Section titled “Direct HTTP access”The HTTP SQL API is useful for local experiments and server-to-server tools. It accepts SQL text rather than bound parameters. Permissive CORS does not provide authorization, and a password included in a browser bundle is visible to its users.
This site uses Google Analytics cookies to count visits. Privacy · License