Basic Usage

Learn how to set up URPC with plugins and middleware

Quick Start

Get started with URPC in seconds using our CLI tool:

bash npx @unilab/urpc-cli create my-project
bash yarn create @unilab/urpc-cli my-project
bash bunx @unilab/urpc-cli create my-project

Manual Setup

Install Dependencies

npm install @unilab/urpc-hono @unilab/urpc @unilab/urpc-core @unilab/mastra-plugin

Create Server

import { URPC } from "@unilab/urpc-hono";
import { MastraPlugin } from "@unilab/mastra-plugin";
import { getMastraInstance } from "@unilab/mastra-plugin/agents";
import { logging } from "@unilab/urpc-core/middleware";
import { MockAdapter } from "@unilab/urpc-adapters";
import { UserEntity } from "./entities/user";

const MyPlugin = {
  entities: [UserEntity],
};

const app = URPC.init({
  plugins: [
    MyPlugin,
    MastraPlugin({
      mastraInstance: getMastraInstance({
        URPC,
        openrouterApiKey: process.env.OPENROUTER_API_KEY!,
        // debug: true,
      }),
      defaultAgent: "urpcSimpleAgent",
    }),
  ],
  middlewares: [logging()],
  entityConfigs: {
    user: {
      defaultSource: "mock",
    },
  },
  globalAdapters: [
    {
      source: "mock",
      factory: () => new MockAdapter(),
    },
  ],
});

export default {
  port: 3000,
  fetch: app.fetch,
};

Use repo on the server side

import { URPC } from "@unilab/urpc-hono";
import { UserEntity } from "./entities/user";

await URPC.repo<UserEntity>({
  entity: "user",
  source: "mock",
}).findOne({
  where: {
    id: "1",
  },
});

Client Usage

import { repo, URPC } from "@unilab/urpc";
import { ChatEntity } from "@unilab/mastra-plugin/entities";
import { UserEntity } from "./entities/user";

URPC.init({
  baseUrl: "http://localhost:3000",
  timeout: 10000,
});

// Traditional entity operations
const user = await repo<UserEntity>({
  entity: "user",
  source: "mock",
}).findOne({
  where: {
    id: "1",
  },
});

// AI-powered natural language queries
const aiResult = await repo<ChatEntity>({
  entity: "chat",
  source: "mastra",
}).call({
  input: "Find all users",
  model: "google/gemini-2.0-flash-001",
});

console.log(aiResult.output);