JavaScript SDK

The official @zmesh/js client library for JavaScript and TypeScript. Auth, Database, AI, Storage, Functions, and Realtime — in one package.

Installation

npm install @zmesh/js

Initialize

Create a client with your project URL, API key, and project ID. You can find these in the Dashboard under Settings → API Keys.

TypeScript

import { createClient } from "@zmesh/js";

const zmesh = createClient({
  url: "https://api.zmesh.in",
  apiKey: "your-project-api-key",
  projectId: "your-project-id",
});

Auth

Full authentication — email/password, OTP (SMS & WhatsApp), and OAuth.

Sign Up

const { user, access_token } = await zmesh.auth.signUp(
  "user@example.com",
  "securePassword123",
  "John Doe" // optional full name
);

Sign In

const { user, access_token } = await zmesh.auth.signIn(
  "user@example.com",
  "securePassword123"
);

OTP (SMS / WhatsApp)

// Send OTP
await zmesh.auth.sendOtp({
  channel: "whatsapp", // or "sms"
  phone: "+919876543210",
});

// Verify OTP
const tokens = await zmesh.auth.verifyOtp({
  channel: "whatsapp",
  phone: "+919876543210",
  code: "123456",
});

OAuth (Google / GitHub)

// Redirect user to OAuth provider
window.location.href = zmesh.auth.getOAuthUrl("google");
// or
window.location.href = zmesh.auth.getOAuthUrl("github");

Get User & Refresh

// Get current user
const user = await zmesh.auth.getUser();

// Refresh token
const newTokens = await zmesh.auth.refreshToken(refreshToken);

// Sign out
zmesh.auth.signOut();

Database

Query your project database with a chainable query builder — like Supabase.

Select & Filter

const { data, total } = await zmesh.db
  .from("posts")
  .select("id, title, created_at")
  .eq("status", "published")
  .order("created_at", { ascending: false })
  .limit(10);

Insert

const { data: post } = await zmesh.db
  .from("posts")
  .insert({ title: "Hello World", content: "My first post" });

Update

await zmesh.db
  .from("posts")
  .update({ title: "Updated Title" })
  .eq("id", "some-uuid");

Delete

await zmesh.db
  .from("posts")
  .delete()
  .eq("id", "some-uuid");

All Filters

.eq("col", value)       // equals
.neq("col", value)      // not equals
.gt("col", value)       // greater than
.gte("col", value)      // greater than or equal
.lt("col", value)       // less than
.lte("col", value)      // less than or equal
.like("col", "%pattern%")
.ilike("col", "%pattern%")  // case-insensitive
.in("col", [1, 2, 3])       // in array

// Chaining
.order("col", { ascending: false })
.limit(25)
.offset(50)

AI Gateway

Use any LLM (OpenAI, Anthropic, Google, Groq, Mistral, DeepSeek) through a single API.

Chat Completion

const response = await zmesh.ai.chat({
  model: "gpt-4o",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "What is zMesh?" },
  ],
  temperature: 0.7,
});

console.log(response.content);
console.log(response.usage); // { prompt_tokens, completion_tokens }

Streaming

for await (const chunk of zmesh.ai.chatStream({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Write a poem about code" }],
})) {
  if (chunk.type === "chunk") {
    process.stdout.write(chunk.content ?? "");
  }
}

Storage

Upload, list, and manage files for your project.

// Upload a file
const result = await zmesh.storage.upload(file, "avatars/");
console.log(result.url);

// List files in a folder
const { files, folders } = await zmesh.storage.list("avatars/");

// Get public URL
const { url } = await zmesh.storage.getUrl("avatars/photo.jpg");

// Delete a file
await zmesh.storage.remove("avatars/photo.jpg");

Edge Functions

Invoke serverless functions deployed to your project.

const result = await zmesh.functions.invoke("send-welcome-email", {
  userId: "abc123",
  template: "onboarding",
});

console.log(result.data);   // function output
console.log(result.status); // "success" | "error" | "timeout"

Realtime

Subscribe to database changes, broadcast messages, and track presence — via WebSocket.

Table Changes

// Connect to realtime
zmesh.realtime.connect();

// Listen for new rows in "messages" table
const unsubscribe = zmesh.realtime.onTable("messages", "INSERT", (msg) => {
  console.log("New message:", msg.data);
});

// Listen for all changes on "orders"
zmesh.realtime.onTable("orders", "*", (msg) => {
  console.log(msg.event, msg.data); // INSERT, UPDATE, or DELETE
});

// Stop listening
unsubscribe();

Broadcast & Presence

// Broadcast a message to a channel
zmesh.realtime.broadcast("chat-room", { text: "Hello!" });

// Listen for broadcasts
zmesh.realtime.onBroadcast("chat-room", (msg) => {
  console.log(msg.payload);
});

// Announce presence
zmesh.realtime.presence({ status: "online", user: "John" });

// Disconnect when done
zmesh.realtime.disconnect();

Error Handling

import { ZMeshError } from "@zmesh/js";

try {
  await zmesh.auth.signIn("bad@email.com", "wrong");
} catch (err) {
  if (err instanceof ZMeshError) {
    console.log(err.status); // 401
    console.log(err.body);   // { detail: "Invalid credentials" }
  }
}

TypeScript Support

Full type definitions included. All response types are exported:

import type {
  AuthTokens,
  AuthUser,
  ChatMessage,
  ChatResponse,
  StreamChunk,
  UploadResult,
  InvokeResult,
} from "@zmesh/js";

Quick Reference

ModuleMethods
zmesh.authsignUp, signIn, sendOtp, verifyOtp, getUser, refreshToken, getOAuthUrl, signOut
zmesh.dbfrom(table).select().eq().neq().gt().lt().like().order().limit().offset() / insert() / update() / delete()
zmesh.aichat, chatStream
zmesh.storageupload, list, getUrl, remove
zmesh.functionsinvoke
zmesh.realtimeconnect, onTable, onBroadcast, broadcast, presence, disconnect