--- slug: javascript-client title: JsonServiceClient --- The **@servicestack/client** library enables the best end-to-end typed developer experience for calling ServiceStack APIs in any TypeScript or JavaScript project. ## Usage in TypeScript or npm projects Projects using TypeScript or any npm build system can install the dependency-free library with: ::: sh npm install @servicestack/client ::: Which can be used with your APIs typed DTOs that TypeScript projects can generate using [TypeScript Add ServiceStack Reference](/typescript-add-servicestack-reference#typescript-serviceclient) with: ::: sh `x ts https://localhost:5001` ::: Which will save your API DTOs to **dtos.ts** that can be referenced in your code with: ```ts import { Hello } from "./dtos" ``` ### JavaScript npm projects Non-TypeScript npm projects can choose to either have TypeScript generate the DTOs into the preferred JavaScript target: ::: sh tsc dtos.ts ::: Alternatively they can generate ES6 annotated DTOs using [JavaScript Add ServiceStack Reference](/javascript-add-servicestack-reference) with: ::: sh `x mjs https://localhost:5001` ::: Which will save your API DTOs to **dtos.mjs** where they can be referenced in your code with: ```ts import { Hello } from "./dtos.mjs" ``` ### Example Usage Either solution gives you the same productive end-to-end Typed API access, e.g: ```ts import { JsonServiceClient } from "@servicestack/client" const client = new JsonServiceClient() const api = await client.api(new Hello({ Name: 'World' })) if (api.succeeded) { console.log(api.response.result) } else { console.log(api.error) } ``` ## Usage in .NET Apps without npm Modern JavaScript Apps not using an npm build system like the [Razor Vue Tailwind templates](/vue/#getting-started) can download **@servicestack/client** from: - https://unpkg.com/@servicestack/client@2/dist/servicestack-client.mjs - https://unpkg.com/@servicestack/client@2/dist/servicestack-client.min.mjs (minified) Then use an [importmap](/javascript-add-servicestack-reference#import-maps) to specify where to load **@servicestack/client** from, e.g: ```html ``` ### ImportMap in Razor Pages or MVC Razor Pages or MVC projects can use `@Html.ImportMap()` in **_Layout.cshtml** to use different builds for development and production, e.g: ```csharp @if (Context.Request.Headers.UserAgent.Any(x => x.Contains("Safari") && !x.Contains("Chrome"))) { } @Html.ImportMap(new() { ["@servicestack/client"] = ("/js/servicestack-client.mjs", "/js/servicestack-client.min.mjs"), }) ``` This lets your source code reference the library by package name to enable using the same source code in a [JavaScript Module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), e.g: ```html ``` ### JavaScript API DTOs Your [JavaScript API DTOs](/javascript-add-servicestack-reference) can either directly reference the **/types/mjs** endpoint: ```js import { Hello } from '/types/mjs' ``` ### Enable static analysis and intelli-sense Or for better IDE intelli-sense during development, save the annotated Typed DTOs to disk with the [x dotnet tool](/dotnet-tool): :::sh x mjs ::: Then reference it instead to enable IDE static analysis when calling Typed APIs from JavaScript: ```js import { Hello } from '/js/dtos.mjs' client.api(new Hello({ name })) ``` To also enable static analysis for **@servicestack/client**, install the dependency-free library as a dev dependency: :::sh npm install -D @servicestack/client ::: Where only its TypeScript definitions are used by the IDE during development to enable its type-checking and intelli-sense. #### Support Legacy Browsers JavaScript Projects needing to support legacy browsers can use [ES3 Common.js DTOs](/commonjs-add-servicestack-reference) to enable access using old-style `