Skip to main content

Signing Transactions

This is a guide to signing transactions in a server context. While these snippets leverage Ethers, it can be swapped out for other signers in the Viem or Solana contexts. An example for Ethers can be found here, and here for Viem in the server context. A similar example with Solana can be found here.

1. Initialize the Server Client

import { Turnkey } from "@turnkey/sdk-browser";

const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
defaultOrganizationId: process.env.TURNKEY_ORGANIZATION_ID,
apiPrivateKey: process.env.API_PRIVATE_KEY,
apiPublicKey: process.env.API_PUBLIC_KEY,
});

2. Initialize an Ethers Provider and Turnkey Signer

import { ethers } from "ethers";
import { TurnkeySigner } from "@turnkey/ethers";

const provider = new ethers.JsonRpcProvider(<provider api url>);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient.apiClient(),
organizationId: process.env.ORGANIZATION_ID!,
signWith: process.env.SIGN_WITH!,
});

3. Call sendTransaction with the Turnkey Signer

const transactionRequest = {
to: "<destination address>",
value: ethers.parseEther("<amount to send>"),
type: 2,
};
const sendTransaction =
await connectedSigner.sendTransaction(transactionRequest);