JSPM

  • Created
  • Published
  • Downloads 71563
  • Score
    100M100P100Q155423F
  • License Apache-2.0

Typed HTTP client for interacting with Turnkey API

Package Exports

  • @turnkey/http
  • @turnkey/http/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@turnkey/http) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@turnkey/http

npm

A lower-level, fully typed HTTP client for interacting with Turnkey API.

For signing transactions and messages, check out the higher-level @turnkey/ethers Signer.

API Docs: https://turnkey.readme.io/

Getting started

$ npm install @turnkey/http

Before making http calls, initialize the package with your credentials:

import { TurnkeyApi, init } from "@turnkey/http";

init({
  apiPublicKey: "...",
  apiPrivateKey: "...",
  baseUrl: "...",
});

// Now you can make authenticated requests!
const data = await TurnkeyApi.postGetWhoami({
  body: {
    organizationId: "...",
  },
});

HTTP fetchers

@turnkey/http provides fully typed http fetchers for interacting with the Turnkey API. You can find all available methods here. The types of input parameters and output responses are also exported for convenience.

The OpenAPI spec that generates all fetchers is also included in the package.

withAsyncPolling(...) helper

All Turnkey mutation endpoints are asynchronous (with the exception of signing endpoints). To help you simplify async mutations, @turnkey/http provides a withAsyncPolling(...) wrapper. Here's a quick example:

import {
  TurnkeyApi,
  withAsyncPolling,
  TurnkeyActivityError,
} from "@turnkey/http";

// Use `withAsyncPolling(...)` to wrap & create a fetcher with built-in async polling support
const fetcher = withAsyncPolling({
  request: TurnkeyApi.postCreatePrivateKeys,
});

// The fetcher remains fully typed. After submitting the request,
// it'll poll until the activity reaches a terminal state.
try {
  const activity = await fetcher({
    body: {
      /* ... */
    },
  });

  // Success!
  console.log(activity.result.createPrivateKeysResult?.privateKeyIds?.[0]);
} catch (error) {
  if (error instanceof TurnkeyActivityError) {
    // In case the activity is rejected, failed, or requires consensus,
    // a rich `TurnkeyActivityError` will be thrown. You can read from
    // `TurnkeyActivityError` to find out why the activity didn't succeed.
    //
    // For instance, if your activity requires consensus and doesn't have
    // enough approvals, you can get the `activityId` from `TurnkeyActivityError`,
    // store it somewhere, then re-fetch the activity via `.postGetActivity(...)`
    // when the required approvals/rejections are in place.
  }
}

More examples

See createNewEthereumPrivateKey.ts in the with-ethers example.

See also