www/versioned_docs/version-10.x/client/vanilla/setup.mdx
import TabItem from '@theme/TabItem'; import Tabs from '@theme/Tabs';
Use your preferred package manager to install the @trpc/client library, and also install @trpc/server which contains some required types.
npm install @trpc/client @trpc/server
yarn add @trpc/client @trpc/server
pnpm add @trpc/client @trpc/server
bun add @trpc/client @trpc/server
import ImportAppRouter from '../../partials/_import-approuter.mdx';
<ImportAppRouter />Create a tRPC client with the createTRPCProxyClient method, and add a links array with a terminating link pointing at your API. To learn more about tRPC links, click here.
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from '../path/to/server/trpc';
const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: 'http://localhost:3000/trpc',
// You can pass any HTTP headers you wish here
async headers() {
return {
authorization: getAuthCookie(),
};
},
}),
],
});
Under the hood this creates a typed JavaScript Proxy which allows you to interact with your tRPC API in a fully type-safe way:
const bilbo = await client.getUser.query('id_bilbo');
// => { id: 'id_bilbo', name: 'Bilbo' };
const frodo = await client.createUser.mutate({ name: 'Frodo' });
// => { id: 'id_frodo', name: 'Frodo' };
You're all set!