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

👍

Difficulty: Easy

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://ethereum-sepolia-rpc.publicnode.com) 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)

❗️

Difficulty: Harder

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

We recommend first setting up wagmi in your application as a Web3 provider. Next, we recommend using Dynamic so that users can log in to their preferred wallet. Ultimately you will end up with something that looks like this:

ℹ️ Make sure to have NEXT_PUBLIC_RPC_PROVIDER_URL for your desired chain set up in your .env file. You can use a public default one (NEXT_PUBLIC_RPC_PROVIDER_URL=https://ethereum-sepolia-rpc.publicnode.com) 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.

ℹ️ Make sure to have NEXT_PUBLIC_DYNAMIC_ENV_ID set up in your .env file. Do this by logging into Dynamic and creating a project.

"use client";
import { http, createConfig, WagmiProvider } from "wagmi";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { sepolia } from "wagmi/chains";
import { DynamicContextProvider } from "@dynamic-labs/sdk-react-core";
import { DynamicWagmiConnector } from "@dynamic-labs/wagmi-connector";
import { EthereumWalletConnectors } from "@dynamic-labs/ethereum";

// setup wagmi
const config = createConfig({
  chains: [sepolia],
  multiInjectedProviderDiscovery: false,
  transports: {
    [sepolia.id]: http(),
  },
});
const queryClient = new QueryClient();
// setup dynamic
const evmNetworks = [
  {
    blockExplorerUrls: ["https://sepolia.etherscan.io"],
    chainId: 11155111,
    iconUrls: ["https://app.dynamic.xyz/assets/networks/sepolia.svg"],
    name: "Sepolia",
    nativeCurrency: {
      decimals: 18,
      name: "Sepolia Ether",
      symbol: "ETH",
    },
    networkId: 11155111,
    rpcUrls: [process.env.NEXT_PUBLIC_RPC_PROVIDER_URL],
    vanityName: "Sepolia",
  },
];

export default function Web3Providers({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <DynamicContextProvider
      settings={{
        appName: "Story Documentation",
        // Find your environment id at https://app.dynamic.xyz/dashboard/developer
        environmentId: process.env.NEXT_PUBLIC_DYNAMIC_ENV_ID as string,
        walletConnectors: [EthereumWalletConnectors],
        overrides: { evmNetworks },
        networkValidationMode: "always",
      }}
    >
      <WagmiProvider config={config}>
        <QueryClientProvider client={queryClient}>
          <DynamicWagmiConnector>
            {children}
          </DynamicWagmiConnector>
        </QueryClientProvider>
      </WagmiProvider>
    </DynamicContextProvider>
  );
}
import Web3Providers from "./Web3Providers";

export default function RootLayout({
  children
}) {
  return (
    <html lang="en">
      <body>
        <Web3Providers>{children}</Web3Providers>
      </body>
    </html>
  );
}
import { custom } from 'viem';
import { StoryClient, StoryConfig } from "@story-protocol/core-sdk";
import { useWalletClient } from "wagmi";

export default function TestComponent() {
  const { data: wallet } = useWalletClient();
  
  function setupStoryClient() {
    const config: StoryConfig = {
      account: wallet.account,
      ransport: custom(wallet.transport),
      chainId: "sepolia"
    };
    const client = StoryClient.newClient(config); 
    return client;
  }
  
  return (
    {/* */} 
  )
}