# Browser applications

Connect a browser application through your backend while keeping ParticleDB credentials and query permissions on the server.

Source: https://particledb.ai/sdks/web/

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`](/sdks/typescript/#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

This example calls an application route, not the database directly:

```javascript
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

A Node.js backend can also use the native [`@particledb/sdk`](/sdks/typescript/#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](/getting-started/quickstart/). Set `DATABASE_URL` in the backend environment using your deployment's SQL-client address and TLS settings.

```bash
npm install pg
```

Save this as `server.mjs`:

```javascript
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

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](/sdks/typescript/) for parameterized queries and transactions.

## Direct HTTP access

The [HTTP SQL API](/connectivity/http-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.

[Security](/guides/security/) · [HTTP API](/connectivity/http-api/) · [Node.js](/sdks/typescript/)
