Basic Usage

Learn how to set up URPC with plugins and middleware

Quick Start

Get started with URPC in seconds using our CLI tool:

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

The CLI will guide you through project setup, install dependencies, and start the development server automatically.

Manual Setup

If you prefer to set up URPC manually:

Install Dependencies

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

Create Server

// server.ts
import { URPC } from "@unilab/urpc-hono";
import { MastraPlugin } from "@unilab/mastra-plugin/hono";
import { Logging } from "@unilab/urpc-core/middleware";
import { UserEntity } from "./entities/user";
import { UserAdapter } from "./adapters/user";
import { MockAdapter } from "@unilab/urpc-adapters";

const MyPlugin = {
  entities: [UserEntity],
  // adapters: [
  //   {
  //     entity: "UserEntity",
  //     source: "demo",
  //     adapter: new UserAdapter(),
  //   },
  // ],
};

const app = URPC.init({
  plugins: [
    MyPlugin,
    MastraPlugin({
      defaultModel: "openai/gpt-4o-mini",
      openrouterApiKey: process.env.OPENROUTER_API_KEY,
      debug: true,
    }),
  ],
  middlewares: [Logging()],
  entityConfigs: {
    user: {
      defaultSource: "mock",
    },
  },
  globalAdapters: [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

// client.ts
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);

Running the Server

bun run server.ts
# or
node server.js

Next Steps