DEV Community

Alex Spinov
Alex Spinov

Posted on

PocketBase Has a Free API You're Not Using

PocketBase is a backend-as-a-service in a single Go binary. SQLite database, REST API, real-time subscriptions, auth, file storage — all in one 15MB executable.

The Free APIs You're Missing

1. Real-Time Subscriptions — Live Data

import PocketBase from "pocketbase";

const pb = new PocketBase("http://localhost:8090");

// Subscribe to ALL changes on a collection
pb.collection("posts").subscribe("*", (e) => {
  console.log(e.action); // "create" | "update" | "delete"
  console.log(e.record);
});

// Subscribe to specific record
pb.collection("posts").subscribe("RECORD_ID", (e) => {
  console.log("Record updated:", e.record);
});

// Unsubscribe
pb.collection("posts").unsubscribe();
Enter fullscreen mode Exit fullscreen mode

2. Expand Relations — Join Without SQL

// Fetch posts with nested author and comments
const posts = await pb.collection("posts").getList(1, 20, {
  expand: "author,comments(post)",
  filter: "published = true",
  sort: "-created",
});

posts.items.forEach(post => {
  console.log(post.title);
  console.log(post.expand?.author?.name);
  console.log(post.expand?.["comments(post)"]?.length);
});
Enter fullscreen mode Exit fullscreen mode

3. Auth System — Built-In User Management

// Email/password auth
await pb.collection("users").authWithPassword("user@example.com", "password123");

// OAuth2
await pb.collection("users").authWithOAuth2({ provider: "google" });

// Auth state
console.log(pb.authStore.isValid);
console.log(pb.authStore.model);

// Auto-refresh
pb.authStore.onChange((token, model) => {
  console.log("Auth changed:", model?.email);
});
Enter fullscreen mode Exit fullscreen mode

4. File Handling — Built-In File Storage

// Upload files
const formData = new FormData();
formData.append("title", "My Post");
formData.append("image", fileInput.files[0]);
formData.append("attachments", file1);
formData.append("attachments", file2);

const record = await pb.collection("posts").create(formData);

// Get file URL (with thumb)
const url = pb.files.getURL(record, record.image, { thumb: "200x200" });
Enter fullscreen mode Exit fullscreen mode

5. Hooks — Server-Side Logic in Go or JS

// pb_hooks/main.pb.js
onRecordAfterCreateRequest((e) => {
  // Send notification after new post
  const post = e.record;
  const author = $app.dao().findRecordById("users", post.getString("author"));

  $app.dao().saveRecord(new Record($app.dao().findCollectionByNameOrId("notifications"), {
    user: post.getString("author"),
    message: `Your post "${post.getString("title")}" was published!`,
  }));
}, "posts");
Enter fullscreen mode Exit fullscreen mode

Getting Started

# Download single binary
wget https://github.com/pocketbase/pocketbase/releases/latest/download/pocketbase_linux_amd64.zip
unzip pocketbase_linux_amd64.zip
./pocketbase serve

# Admin UI at http://localhost:8090/_/
Enter fullscreen mode Exit fullscreen mode

Need data from any website delivered as clean JSON? I build production web scrapers that handle anti-bot, proxies, and rate limits. 77 scrapers running in production. Email me: Spinov001@gmail.com

Check out my awesome-web-scraping list for the best scraping tools and resources.

Top comments (0)