PocketBase JavaScript SDK

0.26.8 · active · verified Sun Apr 19

The PocketBase JavaScript SDK is the official client library for interacting with the PocketBase API, supporting both browser and Node.js environments. It provides comprehensive functionalities for data management, user authentication (including password and OAuth2 flows), and real-time subscriptions. The current stable version is 0.26.8, with the project maintaining a frequent release cadence to deliver bug fixes, performance improvements, and feature parity with the core PocketBase server. Key differentiators include its seamless integration with the PocketBase ecosystem, robust type definitions, flexible authentication store options (e.g., `LocalAuthStore` for web, `AsyncAuthStore` for React Native), and a crucial `pb.filter()` helper for securely binding filter parameters to prevent string injection attacks when handling untrusted server-side input.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the PocketBase client, authenticating a user, fetching and filtering records, and creating a new record. Includes necessary polyfills for Node.js compatibility.

import PocketBase from 'pocketbase';
import 'cross-fetch/polyfill'; // Only for Node < 17

const pb = new PocketBase('http://127.0.0.1:8090');

async function initializeApp() {
  try {
    // Example: Authenticate as an auth collection record
    const userData = await pb.collection('users').authWithPassword('test@example.com', '123456');
    console.log('User authenticated:', userData.record.email);

    // Example: List and filter 'posts' collection records
    const result = await pb.collection('posts').getList(1, 20, {
      filter: pb.filter('status = true && created > {:date}', { date: new Date('2023-01-01T00:00:00Z') }),
      sort: '-created'
    });
    console.log('Posts found:', result.items.length);

    // Example: Create a new record
    const newPost = await pb.collection('posts').create({
      title: 'My new post',
      content: 'This is some content.',
      status: true,
      author: userData.record.id
    });
    console.log('New post created:', newPost.id);

  } catch (error) {
    console.error('An error occurred:', error);
    if (error.isAbort) {
        console.error('Request was aborted.');
    }
  }
}

initializeApp();

view raw JSON →