This example shows how to construct and sign a transaction using Turnkey's @turnkey/viem
signer, using API keys.
If you want to see a demo with passkeys, head to the example with-viem-and-passkeys
to see a NextJS app using passkeys.
Make sure you have Node.js
installed locally; we recommend using Node v16+.
$ git clone https://github.com/tkhq/sdk
$ cd sdk/
$ corepack enable # Install `pnpm`
$ pnpm install -r # Install dependencies
$ pnpm run build-all # Compile source code
$ cd examples/with-viem/
The first step is to set up your Turnkey organization and account. By following the Quickstart guide, you should have:
- A public/private API key pair for Turnkey
- An organization ID
- A (crypto) private key ID
Once you've gathered these values, add them to a new .env.local
file. Notice that your private key should be securely managed and never be committed to git.
$ cp .env.local.example .env.local
Now open .env.local
and add the missing environment variables:
API_PUBLIC_KEY
API_PRIVATE_KEY
BASE_URL
ORGANIZATION_ID
PRIVATE_KEY_ID
$ pnpm start
This script will do the following:
- construct a new transaction
- sign it with Turnkey
- broadcast it via Infura
Sample output from a successful execution:
$ pnpm start
Fetching Node.js 18.0.0 ...
> @turnkey/[email protected] start /Users/rno/tkhq/code/sdk/examples/with-viem
> pnpm -w run build-all && tsx src/index.ts
> @turnkey/oss@ build-all /Users/rno/tkhq/code/sdk
> tsc --build tsconfig.mono.json
Source address
0xfe81d134AC28b5CAc01E198E5988F449621d960B
Transaction
https://sepolia.etherscan.io/tx/0x91a388d8a6e506bbaeaf21b2914461a0f6125afb3a84a4cf27bdf5e9eb06ffce