Core Quickstart
If you're not using React, but still want a number of the abstractions that make the React package so easy to use, you can leverage the @account-kit/core
package directly.
In this guide, we'll walk you through how to use this package to send a user operation, while using the reactive utilities exported by this package.
Install packages
Prerequisites- minimum Typescript version of 5
- pin viem to 2.20.0 (
yarn add [email protected]
) - pin wagmi to 2.12.7 (
yarn add [email protected]
)
To get started, you'll need to install the required packages. We also install the infra
package because it contains the necessary Chain
definitions that makes it easier to setup
your a Bundler client.
yarn add @account-kit/core @account-kit/infra
Get your API keys
- Get your API key by creating a new app in your Alchemy Dashboard
- Create a new account config in your Alchemy Accounts Manager Dashboard
- Apply the config to your app from step 1
- Use http://localhost:3000 as the “Redirect URL” for testing (***make sure to set http NOT https for local testing)
- Determine the authentication methods you want to support.
- If you are using email auth, you can optionally stylize ✨ the email with your brand color and logo!
- If you want to enable social login, toggle which auth providers you want to support, optionally enter your own Oauth credentials or use our defaults, and use http://localhost:3000 as a whitelisted origin for testing.
- Copy the API Key found at the end of the URL (you don't need the whole link for this demo)
Create a config
Now, you're ready to create a config. The config we create should be a static object that you can import anywhere into your application. It contains all of the state that the functions within this package use.
import { createConfig } from "@account-kit/core";
import { alchemy, sepolia } from "@account-kit/infra";
export const config = createConfig({
transport: alchemy({ apiKey: "YOUR_API_KEY" }),
chain: sepolia,
// optional if you want to sponsor gas
policyId: "YOUR_POLICY_ID",
});
Authenticate the user
Before you can create a Smart Account instance for your users, you need to authenticate them with the user. Depending on what framework you're using this will look different, but using email based auth as an example you would:
- collect the user's email
- call the
authenticate
method on the signer - handle the redirect from the user's email and pass the bundle to the signer to complete login
import { config } from "./config";
import { getSigner } from "@account-kit/core";
const signer = getSigner(config);
if (!signer) {
// this can happen if your rendering this on the server
// the signer instance is only available on the client
throw new Error("Signer not found");
}
// authenticate the user with email
await signer.authenticate({
type: "email",
email: "user@email.com",
});
// once the user has clicked on the email and been redirected back to your site
const bundle = new URLSearchParams(window.location.search).get("bundle");
if (!bundle) {
throw new Error("No bundle found in URL");
}
await signer.authenticate({ type: "email", bundle });
Send a user operation
Now that you have your config, you can send user operations by leveraging the underlying smart account client.
import { watchSmartAccountClient } from "@account-kit/core";
import { config } from "./config";
let clientState;
// The watch smart account client will handle all of the possible state changes
// that can impact this client:
// - Signer status
// - Account instantiation
// - Chain changes
const clientSubscription = watchSmartAccountClient(
{
type: "LightAccount",
},
config
)((clientState_) => {
clientState = clientState_;
});
if (clientState == null || clientState.isLoadingClient) {
console.log("Loading...");
}
const client = clientState.client;
await client.sendUserOperation({
uo: {
target: "0xtarget",
data: "0x",
value: 0n,
},
});
The key thing here is the watchSmartAccountClient
method which allows you to subscribe to the state of signer and underlying account to give you a stable instance of the Smart Account Client. How you store the clientState
variable will depend largely on
your framework, but the above example should give you a good starting point.