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 to host the node project called my-story-protocol-example:

mkdir my-story-protocol-example

Go into the folder and run npm init to initialize the project:

cd my-story-protocol-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 my-story-protocol-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/[email protected] [email protected]
yarn add @story-protocol/[email protected] [email protected]

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

There are two ways to set up an account. The first is to use a private key locally:

ℹ️ 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 a public default one (RPC_PROVIDER_URL=https://rpc.ankr.com/eth_sepolia) or, for a much faster experience, sign up for a free Alchemy account and create an Ethereum Sepolia test application to get your own private RPC URL. Once your application is created, you can click the "API Key" button and then copy the link in the HTTP box.

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: 'sepolia'
};
export const client = StoryClient.newClient(config);

Set Up JSON-RPC Account (ex. Metamask)

The second way is to delay signing to a JSON-RPC account like Metamask.

import { custom } from 'viem';
import { Address } from 'viem/accounts';
import { StoryClient, StoryConfig } from "@story-protocol/core-sdk";

if (window.ethereum) {
  const [account]: [Address] = await window.ethereum.request({
    method: "eth_requestAccounts",
  });
  
  const config: StoryConfig = {
    transport: custom(window.ethereum),
    account: account, // the account address from above
    chainId: 'sepolia'
  };
  export const client = StoryClient.newClient(config);
}