Skip to main content

TurnkeyClient

Introduction

The @turnkey/http package is a fully-typed lower-level HTTP client for developers integrating with Turnkey.

Installing

To get started install the @turnkey/http client.

npm i @turnkey/http

Initializing

Create a new client for use in your JavaScript/Typescript applications.

You can initialize a new TurnkeyClient using the TurnkeyClient constructor. The TurnkeyClient serves as your entry point to interact with the Turnkey API.

Parameters

configTHttpConfigrequired

An object containing configuration settings for the client.

.baseUrlstringrequired

The base URL for the Turnkey API. Note: An error Missing base URL. Please verify env vars. will be thrown if a value is not provided.

stamperTStamperrequired

An instance of a stamper class (e.g. ApiKeyStamper) used to create signatures for authenticating API requests.

Currently Turnkey provides 3 stampers:

  • applications signing requests with Passkeys or webauthn devices should use @turnkey/webauthn-stamper
  • applications signing requests with API keys should use @turnkey/api-key-stamper
  • applications that need to sign requests within an iframe, particularly when handling sensitive operations like Recovery and Auth, or Key or Wallet Export, should use the @turnkey/iframe-stamper.

You can also implement the TStamper interface yourself. For more information on implementing a custom stamper checkout the API Design docs.

Types

THttpConfig

type THttpConfig = {
baseUrl: string;
};

TStamper

interface TStamper {
stamp: (input: string) => Promise<TStamp>;
}

Example

import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";

// Following best practices, define parameters in your .env file
const baseUrl = process.env.TURNKEY_BASE_URL || "https://api.turnkey.com";
const apiPublicKey = process.env.TURNKEY_API_PUBLIC_KEY;
const apiPrivateKey = process.env.TURNKEY_API_PRIVATE_KEY;

// Initialize the API key stamper
const stamper = new ApiKeyStamper({ apiPublicKey, apiPrivateKey });

// Initialize the Turnkey client and then you're ready to use the Turnkey client! 🎉
const client = new TurnkeyClient({ baseUrl }, stamper);