Skip to content

Commit

Permalink
WIP: Adds documentation for sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorjdawson committed Jan 12, 2024
1 parent b77d963 commit d80e574
Show file tree
Hide file tree
Showing 12 changed files with 769 additions and 311 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
build
*.sw[a-z]
.DS_Store
.gitconfig
.gitconfig
.vscode
1 change: 1 addition & 0 deletions docs/api-design/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Every request made to Turnkey must include a signature inside a stamp header. Ou
1. Create a JSON-encoded string with the authenticator data, client data, credential ID, and signature.
2. Add the string to your request as a `X-Stamp-Webauthn` header

### If using a custom stamper
In practice you should not have to worry about this step: our [JS SDK](https://github.com/tkhq/sdk) and [CLI](https://github.com/tkhq/tkcli) will take care of it for you. However, if you write an independent client, you will need to implement this yourself.

For reference, check out our implementations:
Expand Down
3 changes: 2 additions & 1 deletion docs/sdk/_category_.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"label": "SDK",
"position": 7,
"link": {
"type": "generated-index"
"type": "generated-index",
"description": "Discover how to effectively utilize the Turnkey SDK in your applications, with comprehensive information on setup, usage, and best practices for integrating Turnkey's capabilities seamlessly."
}
}
192 changes: 192 additions & 0 deletions docs/sdk/api-key-stamper.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
title: "ApiKeyStamper"
sidebar_position: 2
description: Guide on using the ApiKeyStamper.
slug: /sdk/api-key-stamper
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import CodeBlock from '@theme/CodeBlock'
import Parameter from '@site/src/components/parameter'

## Introduction

The `@turnkey/api-key-stamper` module is an abstraction for stamping requests made to Turnkey's API using your public and private API keys.
For more information on API keys refer to [this section](../faq#why-do-you-require-a-public--private-key-pair-to-access-turnkey-api).

The stamper can be used to initiate select requests on behalf of the end-user.
For instance, your app can create a new sub-organization on behalf of the end-user. See our integration guide
[Sub-Organizations as Wallets](../integration-guides/sub-organizations-as-wallets#step-1-create-a-sub-organization) for a complete example.

:::note

This module is intended to be used on the **server** as you would **_not_** want to expose your private API key to the client.

:::

## Installing

To get started install the [`@turnkey/api-key-stamper`](https://www.npmjs.com/package/@turnkey/api-key-stamper) client.

<Tabs>
<TabItem value="npm" label="npm" default>
<CodeBlock
language="bash">
{`npm i @turnkey/api-key-stamper`}
</CodeBlock>
</TabItem>
<TabItem value="pnpm" label="pnpm">
<CodeBlock
language="bash">
{`pnpm i @turnkey/api-key-stamper`}
</CodeBlock>
</TabItem>
<TabItem value="yarn" label="yarn">
<CodeBlock
language="bash">
{` yarn add @turnkey/api-key-stamper`}
</CodeBlock>
</TabItem>
</Tabs>


## Initializing
The **`ApiKeyStamper`** class is a helper, which abstracts away the required stamp headers when making request to Turnkey's API.
You can initialize a new **`ApiKeyStamper`** using the **`ApiKeyStamper`** constructor:

### `constructor(config: TApiKeyStamperConfig): TStamper`

The `ApiKeyStamper` class is responsible for creating a digital stamp using API keys.
This stamp is used for authenticating requests made with the `@turnkey/http`'s `TurnkeyClient`.

#### Parameters



<Parameter
style={{ borderBottom: "none", paddingBottom: "none"}}
param={{
name: 'config',
type: {
name: 'TApiKeyStamperConfig',
link: 'https://github.com/tkhq/sdk/blob/main/packages/api-key-stamper/src/index.ts#L8-L11'
}
}}
isRequired
>

An object containing configuration settings for the stamper.

</Parameter>


<Parameter
style={{ paddingTop: '0', paddingLeft: "12px"}}
param={{
name: '.apiPrivateKey',
type: {
name: 'string',
}
}}
isRequired
>

Your Turnkey api private key.

</Parameter>

<Parameter
style={{ paddingLeft: "12px"}}
param={{
name: '.apiPublicKey',
type: {
name: 'string',
}
}}
isRequired
>

Your Turnkey api public key.

</Parameter>



#### Types

##### `TApiKeyStamperConfig`
```ts
type TApiKeyStamperConfig = {
apiPublicKey: string;
apiPrivateKey: string;
}
```
##### `TStamper`
```ts
interface TStamper {
stamp: (input: string) => Promise<TStamp>;
}
```

#### Example

The example below shows how to initialze and use the `ApiKeyStamper` with the `TurnkeyClient` to make a request
to Turnkey's [`/public/v1/query/whoami`](https://docs.turnkey.com/api#tag/Sessions/operation/GetWhoami) endpoint:

```ts
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
const tk = new TurnkeyClient({ baseUrl }, stamper)

// Now you can make authenticated requests using the APIKeyStamper
const whoami = await tk.getWhoami({
organizationId: "<Your Org ID>",
})
```

## Methods


### `stamp: (input: string) => Promise<TStamp>`

Creates a digital stamp which includes the public key, signature scheme, and a signature.
This stamp is used in the `X-Stamp` header of HTTP requests to authenticate.

#### Parameters

<Parameter
param={{
name: 'payload',
type: {
name: 'string',
}
}}
isRequired
>

The payload that needs to be stamped.

</Parameter>

#### Types

##### `TStamp`

```ts
type TStamp = {
stampHeaderName: string;
stampHeaderValue: string;
}
```
15 changes: 15 additions & 0 deletions docs/sdk/iframe-stamper.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "IframeStamper"
sidebar_position: 4
description: Guide on using the IframeStamper.
slug: /sdk/iframe-stamper
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import CodeBlock from '@theme/CodeBlock'
import Parameter from '@site/src/components/parameter'

## Introduction

## Installing
2 changes: 1 addition & 1 deletion docs/sdk/libraries.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 1
sidebar_position: 5
description: Turnkey clients and libraries
slug: /sdk/libraries
---
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/troubleshooting.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 6
description: Common issues and errors using Turnkey's SDKs
slug: /sdk-troubleshooting
---
Expand Down
135 changes: 135 additions & 0 deletions docs/sdk/turnkey-client.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
title: "TurnkeyClient"
sidebar_position: 1
description: Detailed guide on installing and initializing the TurnkeyClient
slug: /sdk/turnkey-client
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import CodeBlock from '@theme/CodeBlock'
import Parameter from '@site/src/components/parameter';

## Introduction

## Installing

To get started install the [`@turnkey/http`](https://www.npmjs.com/package/@turnkey/http) client.

<Tabs>
<TabItem value="npm" label="npm" default>
<CodeBlock
language="bash">
{`npm i @turnkey/http`}
</CodeBlock>
</TabItem>
<TabItem value="pnpm" label="pnpm">
<CodeBlock
language="bash">
{`pnpm i @turnkey/http`}
</CodeBlock>
</TabItem>
<TabItem value="yarn" label="yarn">
<CodeBlock
language="bash">
{` yarn add @turnkey/http`}
</CodeBlock>
</TabItem>
</Tabs>

## Initializing

Create a new client for use in your JavaScript applications.

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

### Parameters

<Parameter
style={{ borderBottom: "none", paddingBottom: "none"}}
param={{
name: 'config',
type: {
name: 'THttpConfig',
link: 'https://github.com/tkhq/sdk/blob/main/packages/http/src/base.ts#L257-L259'
}
}}
isRequired
>
{/* Leaving space before/after content is necessary when rendering component child as markdown */}

An object containing configuration settings for the client.

</Parameter>


<Parameter
style={{ paddingTop: '0', paddingLeft: "12px"}}
param={{
name: '.baseUrl',
type: {
name: 'string',
}
}}
isRequired
>

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.

</Parameter>

<Parameter
param={{
name: 'stamper',
type: {
name: 'TStamper',
link: "https://github.com/tkhq/sdk/blob/1b20d5767a913e56174337cbadc7c439c0d400e7/packages/http/src/base.ts#L284-L292"
}
}}
isRequired
>

An instance of a stamper class (e.g. [**`ApiKeyStamper`**](https://github.com/tkhq/sdk/blob/main/packages/api-key-stamper/src/index.ts#L33)) used to create signatures for authenticating API requests.

Currently Turnkey provides 2 stampers:
- applications signing requests with Passkeys or webauthn devices should use [`@turnkey/webauthn-stamper`](https://www.npmjs.com/package/@turnkey/webauthn-stamper)
- applications signing requests with API keys should use [`@turnkey/api-key-stamper`](https://www.npmjs.com/package/@turnkey/api-key-stamper)

You can also implement the TStamper interface yourself. For more information checkout [Using a custom stamper](../api-introduction#if-using-a-custom-stamper).

</Parameter>

### Types

#### `THttpConfig`
```ts
type THttpConfig = {
baseUrl: string;
}
```
#### `TStamper`
```ts
interface TStamper {
stamp: (input: string) => Promise<TStamp>;
}
```

### Example


```ts
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)
```
Loading

0 comments on commit d80e574

Please sign in to comment.