|
| 1 | +--- |
| 2 | +title: Publish Client to NPM |
| 3 | +description: How to publish your oRPC client to NPM for users to consume your APIs as an SDK. |
| 4 | +--- |
| 5 | + |
| 6 | +# Publish Client to NPM |
| 7 | + |
| 8 | +Publishing your oRPC client to NPM allows users to easily consume your APIs as a software development kit (SDK). |
| 9 | + |
| 10 | +::: info |
| 11 | +Before you start, we recommend watching some [publish typescript library to npm tutorials](https://www.youtube.com/results?search_query=publish+typescript+library+to+npm) to get familiar with the process. |
| 12 | +::: |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +You must have a project already set up with oRPC. [Contract First](/docs/contract-first/define-contract) is the preferred approach. If you haven't set one up yet, you can clone an [oRPC playground](/docs/playgrounds) and start from there. |
| 17 | + |
| 18 | +::: info |
| 19 | +In this guide, we'll use [pnpm](https://pnpm.io/) as the package manager and [tsdown](https://tsdown.dev/) for bundling the package. You can use other package managers and bundlers, but the commands may differ. |
| 20 | +::: |
| 21 | + |
| 22 | +## Export & Scripts |
| 23 | + |
| 24 | +First, create a `src/index.ts` file to set up and export your client. |
| 25 | + |
| 26 | +```ts [src/index.ts] |
| 27 | +import { createORPCClient } from '@orpc/client' |
| 28 | +import { RPCLink } from '@orpc/client/fetch' |
| 29 | +import type { ContractRouterClient } from '@orpc/contract' |
| 30 | + |
| 31 | +export function createMyApi(apiKey: string): ContractRouterClient<typeof contract> { |
| 32 | + const link = new RPCLink({ |
| 33 | + url: 'https://example.com/rpc', |
| 34 | + headers: { |
| 35 | + 'x-api-key': apiKey, |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + return createORPCClient(link) |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +::: info |
| 44 | +This example uses [RPCLink](/docs/client/rpc-link) combined with [Contract First](/docs/contract-first/define-contract) to create a client. This is just an example, you can use any other link or client setup that you prefer. |
| 45 | +::: |
| 46 | + |
| 47 | +Next, configure your `package.json` with the necessary fields for publishing to NPM. |
| 48 | + |
| 49 | +```json [package.json] |
| 50 | +{ |
| 51 | + "name": "<package-name>", // [!code highlight] |
| 52 | + "type": "module", |
| 53 | + "version": "0.0.0", // [!code highlight] |
| 54 | + "publishConfig": { |
| 55 | + "access": "public" // [!code highlight] |
| 56 | + }, |
| 57 | + "exports": { |
| 58 | + ".": { |
| 59 | + "types": "./dist/index.d.ts", // [!code highlight] |
| 60 | + "import": "./dist/index.js", // [!code highlight] |
| 61 | + "default": "./dist/index.js" // [!code highlight] |
| 62 | + } |
| 63 | + }, |
| 64 | + "files": [ |
| 65 | + "dist" // [!code highlight] |
| 66 | + ], |
| 67 | + "scripts": { |
| 68 | + "build": "tsdown --dts src/index.ts", // [!code highlight] |
| 69 | + "release": "pnpm publish" // [!code highlight] |
| 70 | + }, |
| 71 | + "dependencies": { |
| 72 | + "@orpc/client": "...", // [!code highlight] |
| 73 | + "@orpc/contract": "..." // [!code highlight] |
| 74 | + // ... other dependencies that `src/index.ts` depends on |
| 75 | + }, |
| 76 | + "devDependencies": { |
| 77 | + "tsdown": "latest", |
| 78 | + "typescript": "latest" |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Build & Publish |
| 84 | + |
| 85 | +After completing the necessary setup, commit your changes and run the following commands to build and publish your client to NPM: |
| 86 | + |
| 87 | +```bash |
| 88 | +pnpm login # if you haven't logged in yet |
| 89 | +pnpm run build |
| 90 | +pnpm run release |
| 91 | +``` |
| 92 | + |
| 93 | +## Install & Use |
| 94 | + |
| 95 | +Once your client is published to NPM, you can install it in your project and use it like this: |
| 96 | + |
| 97 | +```bash |
| 98 | +pnpm add "<package-name>" |
| 99 | +``` |
| 100 | + |
| 101 | +```ts [example.ts] |
| 102 | +import { createMyApi } from '<package-name>' |
| 103 | + |
| 104 | +const myApi = createMyApi('your-api-key') |
| 105 | + |
| 106 | +const output = await myApi.someMethod('input') |
| 107 | +``` |
| 108 | + |
| 109 | +::: info |
| 110 | +This client includes all oRPC client features, so you can use it with any supported integrations like [Tanstack Query](/docs/integrations/tanstack-query). |
| 111 | +::: |
0 commit comments