This guide will walk you through the process of sending a token transfer from your wallet using the Crossmint transactions API. You can transfer various ERC20 tokens using this method. One of the benefits of non-custodial Smart Wallets is that you can hold and send different cryptocurrencies.

Prerequisites

Ensure you have a wallet created. You can follow the Quickstart for server wallets to prepare one. You will need:

  • API Key: Ensure you have your Crossmint API key with the scope wallets:transactions.create.
  • Wallet Locator: The locator of the wallet you want to transfer the tokens from.

Sending the Transaction

We will use the Create Transaction API.

1

First, we need to prepare the transaction that will send 4 USDC to the recipient. This will encode the function call for the transfer function for sending 4 USDC to a recipient.

import { encodeFunctionData, parseUnits } from 'viem';

// USDC contract address on Base
const usdcAddress = '0x036CbD53842c5426634e7929541eC2318f3dCF7e';

// Recipient address and amount to send (4 USDC)
const recipient = '0xRecipientAddress'; // Replace with the recipient's address
const amount = parseUnits('4', 6);      // 4 USDC (6 decimals)

// Encode the data for the transfer function
const data = encodeFunctionData({
    abi: [
        {
            "constant": false,
            "inputs": [
                { "name": "recipient", "type": "address" },
                { "name": "amount", "type": "uint256" }
            ],
            "name": "transfer",
            "outputs": [{ "name": "", "type": "bool" }],
            "type": "function"
        }
    ],
    functionName: 'transfer',
    args: [recipient, amount]
});

console.log('Transaction Data:', data);
2

With our encoded transaction data, we need to set the following parameters for the /transactions endpoint:

  • calls: An array of objects with the following properties:
    • to: The token contract address.
    • value: Set to "0" for token transfers.
    • data: The encoded data generated in the previous step.
  • chain: The chain where the transaction will be sent.
  • signer: One of the wallet’s signers, typically the admin signer.
const walletLocator = '0x...';
const apiKey = 'sk_staging...';
const tokenContractAddress = '0x036CbD53842c5426634e7929541eC2318f3dCF7e';
const recipientAddress = '0x...';
const signerAddress = '0x...';
const encodedData = '0x...'; // Encoded transfer function data

async function sendTransaction() {
    const response = await fetch(`https://api.crossmint.com/2022-06-09/wallets/${walletLocator}/transactions`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-API-KEY': apiKey,
        },
        body: JSON.stringify({
            params: {
                calls: [
                    {
                        to: tokenContractAddress,
                        value: '0',
                        data: encodedData,
                    },
                ],
                chain: 'base-sepolia',
                signer: `evm-keypair:${signerAddress}`,
            },
        }),
    });

    const data = await response.json();
    console.log(data);
}

sendTransaction().catch(console.error);