Enoki SDK Examples
The following examples highlight some common workflows for onboarding users using Enoki.
Sponsored transactions using client-side signatures
To sponsor and execute transactions in your backend service using client-side signatures:
-
Log in to the Enoki Portal and select the desired app from your team's dashboard.
-
Create a
publicAPI key enabled for zkLogin and the network you want to target. -
Use the dashboard to add an auth provider and configure the Client ID value it provides.
-
Log in to the Enoki Portal and select the desired app from your team's dashboard.
-
Create a
privateAPI key enabled for Sponsored Transactions and the networks you want to target. -
Create an Enoki client in your backend service using the key you create in the previous step.
Keep your secret key in an
envvariable and take care not to expose it publicly.import { EnokiClient } from '@mysten/enoki'; export const enokiClient = new EnokiClient({ apiKey: process.env.ENOKI_SECRET_KEY!, }); -
Create a new
Transaction, add the needed commands and serialize the transaction kind to bytes (Uint8Array). The following example sets the owner of a kiosk and transfers thekioskOwnerCapto the recipient.const tx = new Transaction(); tx.moveCall({ target: '0x2::kiosk::set_owner_custom', arguments: [tx.object(kioskId), tx.object(user.kioskOwnerCapId), tx.pure(recipient)], }); tx.transferObjects([tx.object(user.kioskOwnerCapId)], recipient); const txBytes = await tx.build({ client: suiClient, onlyTransactionKind: true, }); -
Make a call to the
enokiClientto sponsor the transaction. The response returned will be the bytes and digest of thetransaction, which is later used to execute the transaction:const resp = await enokiClient.createSponsoredTransaction({ network: ENOKI_CLIENT_NETWORK, transactionKindBytes: toB64(txBytes), sender: address, allowedMoveCallTargets: ['0x2::kiosk::set_owner_custom'], allowedAddresses: [recipient], });Set the sender of the transaction, include the move call you are going to invoke in the
allowedMoveCallTargetsfield and the recipient (if any) in theallowedAddressesfield. You can also configure these values in the Enoki Portal project dashboard. Sending these fields requires the API key's Request allowed Move call targets and Request allowed addresses options, which are on by default. See Allow lists. -
Execute the sponsored transaction. You need both the user’s signature and the digest to execute the transaction. The previous call returns the digest but you must collect the signature from the user. Assuming the user session is on the client side, you need to return the transaction bytes to the client and collect the signature as shown:
import { useDAppKit } from '@mysten/dapp-kit-react'; export default function Example() { const dAppKit = useDAppKit(); const getSignature = async () => { // use bytes returned from your backend after sponsoring the transaction const { signature } = await dAppKit.signTransaction({ transaction: bytes }); if (!signature) { throw new Error('Error signing transaction block'); } ... } }The code snippet gets the
bytesvalue from thecreateSponsoredTransaction()response, which it uses to collect the user signature. The code then needs to return that signature to the backend service, along with the digest, to execute the transaction:const resp = await enokiClient.executeSponsoredTransaction({ digest, signature, });
Backend sponsor, execute, and sign transactions
When your backend is the sender, signing with its own key (here an admin key), it can sponsor and
execute in one call with the enoki() client extension from @mysten/enoki. See
Your backend is the sender.
import { enoki } from '@mysten/enoki';
const client = suiClient.$extend(enoki({ apiKey: process.env.ENOKI_PRIVATE_KEY! }));
const tx = new Transaction();
tx.moveCall({
target: '0x2::kiosk::set_owner_custom',
arguments: [tx.object(kioskId), tx.object(kioskOwnerCapId), tx.pure(address)],
});
tx.transferObjects([tx.object(kioskOwnerCapId)], address);
const result = await client.enoki.sponsorAndExecuteTransaction({
transaction: tx,
signer: getSigner(process.env.ADMIN_SECRET_KEY!),
allowedMoveCallTargets: ['0x2::kiosk::set_owner_custom'],
allowedAddresses: [address],
});The transaction never leaves the backend, and Enoki pays gas from its sponsor's address balance.
Migrating a dApp to Mainnet
During dApp development, you typically implement the Enoki workflows in these examples on one of the Sui development networks, Testnet or Devnet. Before migrating an Enoki-enhanced dApp from development networks to Mainnet, check that:
- You set the network parameter that might be passed to the
enokiClient.createAuthorizationURLor to theenokiClient.createSponsoredTransactionfunctions is set tomainnet. - The Sui client you extend with
enoki()points at Mainnet, and anyprepareSponsoredTransactioncalls in the browser passnetwork: 'mainnet'. - In the Enoki Portal, make sure you have Mainnet enabled in the networks list for the API keys that are used for the sponsorship.