# Getting Started

<figure><picture><source srcset="https://1690371742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsivfyCjOhJUkKrxqsPg4%2Fuploads%2FCN2Vd0vezPM2ZYQutQZe%2Fbanner_sdk.png?alt=media&#x26;token=69b8bf1d-ff26-4d88-ae2a-f1fb547cf04a" media="(prefers-color-scheme: dark)"><img src="https://1690371742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsivfyCjOhJUkKrxqsPg4%2Fuploads%2FnnFpQ3FGPVOpSozgTRHP%2Fbanner_sdk_l.png?alt=media&#x26;token=f7bc2e0b-4bb5-4f3e-972f-1467fb995587" alt=""></picture><figcaption></figcaption></figure>

## Pre-requisites

In order to use the D8X Node SDK you will need to have TypeScript and Yarn installed:

{% tabs %}
{% tab title="Linux" %}

* Install Node via nvm, [see here](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)
* Install Typescript:
  * `$ npm install -g typescript`
  * `$ npm install -g ts-node`
* Install Yarn, [see here](https://classic.yarnpkg.com/en/docs/install/#debian-stable)
  {% endtab %}

{% tab title="Windows" %}

* Install node.js with nvm-windows, [see here](https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-windows)
* Install yarn for the installed version of node you plan to use:
  * `nvm use 18.12.1`

    `npm install -g yarn`
* Install Typescript:
  * `$ npm install -g typescript`
  * `$ npm install -g ts-node`
    {% endtab %}
    {% endtabs %}

## Installation

You can install the SDK using NPM or Yarn to access the latest stable version of the package, or by cloning the repo in order to have access to experimental and state-of-the art developments in the D8X API. We recommend using Yarn.

{% tabs %}
{% tab title="Yarn" %}

```shell
$ yarn add @d8x/perpetuals-sdk
```

{% endtab %}

{% tab title="NPM" %}

```shell
$ npm install @d8x/perpetuals-sdk
```

{% endtab %}

{% tab title="Build from source" %}

* Clone the official [D8X Futures Node SDK](https://github.com/D8-X/d8x-futures-node-sdk) repo
* Run `yarn` in the root directory of the repo to install the package
  {% endtab %}
  {% endtabs %}

## Initialization

Once the SDK is installed, import the modules you need from @d8x/d8x-futures-node-sdk`:`

```typescript
//ts-node
import { MarketData, AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
```

Read-only modules, such as MarketData, do not require you to specify a private key and can be instantiated without any additional user-provided data:

```typescript
//ts-node
async function main() {
    // see what chain configurations are available:
    const configs = PerpetualDataHandler.getAvailableConfigs();
    console.log(configs);
    // output of the form:
    // Set(2) { '1101; zkevm', `196; xlayer'}
    
    // load configuration for berachain
    const config = PerpetualDataHandler.readSDKConfig("bera");
    
    // MarketData (read only, no authentication needed)
    let mktData = new MarketData(config);
    
    // Create a proxy instance to access the blockchain
    await mktData.createProxyInstance();
    // now you can access all methods of the Market Data object
}
main();
```

Modules that require the user to cryptographically sign, such as AccountTrade that allows you to post orders on the blockchain, do require you to specify your private key. We recommend setting your private key as an environment variable, e.g. in a terminal prompt:

{% tabs %}
{% tab title="Linux/macOS" %}

<pre class="language-shell"><code class="lang-shell"><strong>$ export PK="your-private-key-without-0x"
</strong></code></pre>

{% endtab %}

{% tab title="Windows" %}

```powershell
> set PK=your-private-key-without-0x
```

{% endtab %}
{% endtabs %}

You can then instantiate modules with write-access as follows:

```typescript
//ts-node
async function main() {
    // load configuration for berachain
    const config = PerpetualDataHandler.readSDKConfig("bera");
    
    // AccountTrade (authentication required)
    const pk: string = <string>process.env.PK;
    let accTrade = new AccountTrade(config, pk);
    
    // Create a proxy instance to access the blockchain
    await accTrade.createProxyInstance();
    // now you can access all methods of the Account Trade object
}
main();
```

#### Config

The D8X Node SDK package provides default configuration files and `readSDKConfig` accepts the choices `"cardona"` (for chainId 2442), `"zkevm"`(for chainId 1101), `"x1"` (for chainId 195) and `"xlayer"` (for chainId 196).  The configuration file is required to initialize the node-package classes. There is most likely no reason to deviate from either of the default configuration files, but if you want you can, by providing a path to your configuration file:

```typescript
//ts-node
// load bespoke configuration
const config = PerpetualDataHandler.readSDKConfig("../config/myConfig.json");
```
