TypeScript SDK Setup
Prerequisite
For running the tutorial for developers, we require node version 20 or later version and npm version 8 to be installed in your environment. To install node and npm, we recommend you go to the Node.js official website and download the latest LTS (Long Term Support) version.
If you want to upgrade node to 20 or higher version, we recommend you to use nvm (Node Version Manager). If you want to upgrade npm to the latest version, you can run the command npm install -g nvm
.
Create a Node Project
Create a folder called story-ts-example
:
mkdir story-ts-example
Go into the folder and run npm init
to initialize the project:
cd story-ts-example/
npm init
You can keep all values as default by hitting enter key when npm init
command prompting you to input package name
, version
,description
etc. After the command is executed, you will see a file named package.json
is created.
Install the Dependencies
In the current folder story-ts-example
, install the Story Protocol SDK node package, as well as viem
(https://www.npmjs.com/package/viem) to access the DeFi wallet accounts.
npm install --save @story-protocol/core-sdk viem
pnpm install @story-protocol/core-sdk viem
yarn add @story-protocol/core-sdk viem
Initiate SDK Client
Next we can initiate the SDK Client by first setting up our account and then the client itself.
Set Up Private Key Account
âšī¸ Make sure to have WALLET_PRIVATE_KEY set up in your .env file.
âšī¸ Make sure to have RPC_PROVIDER_URL for your desired chain set up in your .env file. You can use the public default one (
RPC_PROVIDER_URL=https://testnet.storyrpc.io
) or check out the other RPCs here.
import { http } from 'viem';
import { Account, privateKeyToAccount, Address } from 'viem/accounts';
import { StoryClient, StoryConfig } from "@story-protocol/core-sdk";
const privateKey: Address = `0x${process.env.WALLET_PRIVATE_KEY}`;
const account: Account = privateKeyToAccount(privateKey);
const config: StoryConfig = {
transport: http(process.env.RPC_PROVIDER_URL),
account: account, // the account object from above
chainId: 'iliad'
};
export const client = StoryClient.newClient(config);
Updated 20 days ago