TypeScript SDK Setup
Prerequisite
For running the tutorial for developers, we require node version 18 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.
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. There are two ways to do this:
- Using a private key (preferable for some backend admin)
- JSON-RPC account like Metamask where users sign their own transactions
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://rpc.odyssey.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 = {
account: account, // the account object from above
transport: http(process.env.RPC_PROVIDER_URL),
chainId: 'odyssey'
};
export const client = StoryClient.newClient(config);
Set Up JSON-RPC Account (ex. Metamask)
We can also use the TypeScript SDK to delay signing & sending transactions to a JSON-RPC account like Metamask.
We recommend using wagmi as a Web3 provider and then installing a wallet service like Dynamic, RainbowKit, or WalletConnect. We provide an example for all 3:
Updated about 4 hours ago