Skip to content

createConfig

The createConfig method is used to create a configuration object that is used to initialize the AlchemyAccountProvider. The output of this function contains all of the state that will be used by the various hooks exported by @alchemy/aa-alchemy/react.

WARNING

It's not recommended to use the resulting config directly. However, if you are not using React it is possible to build your own custom hooks using the state contained in the config object.

Import

ts
import { createConfig } from "@alchemy/aa-alchemy/config";

Usage

ts
import { createConfig } from "@alchemy/aa-alchemy/config";
import { sepolia } from "@alchemy/aa-core";

export const config = createConfig({
  // required
  rpcUrl: "/api/rpc",
  chain: sepolia,
});
ts
import { createConfig } from "@alchemy/aa-alchemy/config";
import { base, mainnet, sepolia } from "@alchemy/aa-core";

// the examples use different routes for your chains and signer connection, but you can handle
// this in one route if you prefer
export const config = createConfig({
  // this is the default chain to connect to
  chain: sepolia,
  // this adds configs for all other chains you want to support.
  // NOTE: you must include a config for the default chain as well
  connections: [
    // connection using rpcUrl
    { chain: sepolia, rpcUrl: "/api/rpc/sepolia" },
    // connection using an api key
    { chain: mainnet, apiKey: "ALCHEMY_API_KEY" },
    // connection using a gas manager
    {
      chain: base,
      apiKey: "ALCHEMY_API_KEY",
      gasManagerConfig: { policyId: "POLICY_ID" },
    },
  ],
  // when using multiple chains, you must specify the signer connection config since it might be different than your chain RPC urls
  signerConnection: {
    rpcUrl: "/api/rpc/signer",
  },
});

Parameters

ts
import { type CreateConfigProps } from "@alchemy/aa-alchemy/config";
CreateConfigProps
ts
export type Connection = ConnectionConfig & {
  chain: Chain;
  gasManagerConfig?: AlchemyGasManagerConfig;
};

type RpcConnectionConfig =
  | (Connection & {
      /**
       * Optional parameter that allows you to specify a different RPC Url
       * or connection to be used specifically by the signer.
       * This is useful if you have a different backend proxy for the signer
       * than for your Bundler or Node RPC calls.
       */
      signerConnection?: ConnectionConfig;
      connections?: never;
    })
  | {
      connections: Connection[];
      chain: Chain;
      /**
       * When providing multiple connections, you must specify the signer connection config
       * to use since the signer is chain agnostic and has a different RPC url.
       */
      signerConnection: ConnectionConfig;
    };

export type CreateConfigProps = RpcConnectionConfig & {
  chain: Chain;
  sessionConfig?: AlchemySignerParams["sessionConfig"];
  /**
   * Enable this parameter if you are using the config in an SSR setting (eg. NextJS)
   * Turing this setting on will disable automatic hydration of the client store
   */
  ssr?: boolean;

  // TODO: should probably abstract this out into a function
  storage?: (config?: { sessionLength: number }) => Storage;

  connectors?: CreateConnectorFn[];
} & Omit<
    PartialBy<
      Exclude<AlchemySignerParams["client"], AlchemySignerClient>,
      "iframeConfig"
    >,
    "connection"
  >;
ConnectionConfig
ts
export const ConnectionConfigSchema = z.union([
  z.object({
    rpcUrl: z.never().optional(),
    apiKey: z.string(),
    jwt: z.never().optional(),
  }),
  z.object({
    rpcUrl: z.never().optional(),
    apiKey: z.never().optional(),
    jwt: z.string(),
  }),
  z.object({
    rpcUrl: z.string(),
    apiKey: z.never().optional(),
    jwt: z.never().optional(),
  }),
  z.object({
    rpcUrl: z.string(),
    apiKey: z.never().optional(),
    jwt: z.string(),
  }),
]);

Return Type

ts
import { type AlchemyAccountsConfig } from "@alchemy/aa-alchemy/config";

Returns an object containing the Alchemy Accounts state.

coreStore

CoreStore This store contains all of the state that can be used on either the client or the server.

clientStore

ClientStore This store contains only the state available on the client.