# express-typed-rpc

> Easily create a fully-typed Express JSON RPC API over HTTP. No code generation step required. Inspired by tRPC, but much simpler.

Latest version **2.0.0** (published 2024-01-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-typed-rpc
pnpm add express-typed-rpc
yarn add express-typed-rpc
bun add express-typed-rpc
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-01-29 |
| First published | 2023-05-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Marc H. Weiner |
| Maintainers | mhweiner |
| Keywords | tRPC, RPC, json, typescript, api, http, express, express.js, code-generation, typescript, middleware, type, fully-typed, type-safe, typed-api, express-api, http-api, json-api, node.js, backend, client-server, unit-testing, react, browser-client, web-development, web-api |

## Links

- npm: https://www.npmjs.com/package/express-typed-rpc
- Repository: https://github.com/mhweiner/express-typed-rpc
- Issues: https://github.com/mhweiner/express-typed-rpc/issues
- npm.io page: https://npm.io/package/express-typed-rpc

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2024-01-29
- 2.1.0-next.1 (next) — 2025-08-20
- 1.4.0 — 2024-01-29
- 1.3.2 — 2023-07-18
- 1.3.1 — 2023-07-05
- 1.3.0 — 2023-07-05
- 1.2.0 — 2023-07-05
- 1.1.1 — 2023-06-08
- 1.1.0 — 2023-05-31
- 1.0.6 — 2023-05-31
- 1.0.5 — 2023-05-30
- 1.0.4 — 2023-05-30
- 1.0.3 — 2023-05-30
- 1.0.2 — 2023-05-30
- 1.0.1 — 2023-05-27
- … 1 more at https://npm.io/package/express-typed-rpc/versions

## README

# express-typed-rpc

WARNING! This repo is still a work in progress. Please contribute if you're interested ❤️

[![build status](https://github.com/mhweiner/express-typed-rpc/actions/workflows/release.yml/badge.svg)](https://github.com/mhweiner/express-typed-rpc/actions)
[![semantic-release](https://img.shields.io/badge/semantic--release-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![SemVer](https://img.shields.io/badge/SemVer-2.0.0-blue)]()

Simple express middleware to easily create a fully-typed JSON API over HTTP on both the server-side and client-side.  This project is inspired by tRPC, but much simpler.

**Crazy Simple and Easy to Use 😃**

- Works out of the box with express and Typescript
- No magic or black boxes
- No code generation or build steps! Works 100% statically via Typescript's `infer` keyword
- No writing type specifications
- Minimal configuration
- Client included!
- Tiny codebase (~50LOC) with minimal dependencies. Written in clean, simple Typescript. Contribute or fork and make it your own.

**Make Your Code More Reliable and Go Faster! 🚀**

- Take advantage of Typescript and turn your runtime errors into compiler-time errors! Inputs and outputs are both fully typed.
- Easily unit-test your express handlers since they are now no longer dependent on `req` and `res`

## Installation

```bash
npm i express-typed-rpc
```
 
## Example Usage

### server.ts
```typescript
import express from 'express';
import { Router } from 'express';
import { createAPI, InferAPI } from 'express-typed-rpc/dist/server';

const apiRouter = Router();

const api = {
    greet: (name: string): string => `Hello, ${name}!`,
    multiply: (args: {a: number, b: number}): number => args.a * args.b
};

createAPI(apiRouter, api);

// Export type for use on client
export type API = InferAPI<typeof api>;

const app = express();

app.use('/api', apiRouter);
app.listen(process.env.PORT || 3000);
```

### dom-client.ts
```typescript
import {client} from 'express-typed-rpc/dist/client';
import type {API} from '@yourorg/server' 

const greet = async (name: string): Promise<string> => {
    return await client<API['greet']>('greet', name, {
        endpoint: 'https://api.yourdomain.com',
        options: {} // fetch options (window.RequestInit)
    });
};

const multiply = async (numbers: {a: number, b: number}): Promise<number> => {
    return await client<API['multiply']>('multiply', numbers, {
        endpoint: 'https://api.yourdomain.com',
        options: {} // fetch options (window.RequestInit)
    });
};
```

### node-client.ts
```typescript
import {client} from 'express-typed-rpc/dist/client-node';
import type {API} from '@yourorg/server'

const greet = async (name: string): Promise<string> => {
    return await client<API['greet']>('greet', name, {
        endpoint: 'https://api.yourdomain.com',
        options: {} // https.RequestOptions
    });
};

const multiply = async (numbers: {a: number, b: number}): Promise<number> => {
    return await client<API['multiply']>('multiply', numbers, {
        endpoint: 'https://api.yourdomain.com',
        options: {} // https.RequestOptions
    });
};
```

You must publish your backend as a private repo (Github Packages is recommended). Only the Typescript types are exported/imported and does not affect runtime. You will enjoy the same performance but with IDE autocompletion, validation, 
and compile-time TypeScript errors.

## Contribution

Please contribute to this project! Issue a PR against `main` and request review. 

- Please test your work thoroughly.
- Make sure all tests pass with appropriate coverage.

### How to build locally

```bash
npm i
```

### Running tests

```shell script
npm test
```

---
_Source: https://npm.io/package/express-typed-rpc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
