Getting Public Key Error When Setting Up Candy Machine for Solana NFT Collection

Hey everyone! I’m working on building an NFT marketplace app and trying to set up a candy machine on Solana testnet. Every time I run my code, I keep getting this frustrating error: “Error creating Candy Machine: Error: Expected a Public Key”.

I think the problem might be with how I’m passing the candy machine keypair parameter, but I’ve tried different approaches and nothing works. I tested passing just the public key part and also the full keypair object, but same result.

Here’s my code:

import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { signerIdentity } from '@metaplex-foundation/umi';
import { mplCandyMachine, createCandyMachine } from '@metaplex-foundation/mpl-candy-machine';
import { Keypair, Connection } from '@solana/web3.js';
import fs from 'fs';

// Load my wallet
const walletData = JSON.parse(fs.readFileSync('my-wallet.json', 'utf8'));
const myWallet = Keypair.fromSecretKey(Uint8Array.from(walletData));
console.log('My wallet address:', myWallet.publicKey.toString());

// Setup connection
const rpcConnection = new Connection('https://api.testnet.solana.com');

// Initialize UMI
const umiInstance = createUmi(rpcConnection);
umiInstance.use(signerIdentity(myWallet));
umiInstance.use(mplCandyMachine());

async function setupCandyMachineForNFTs() {
  try {
    const machineKeypair = Keypair.generate();
    
    const machineConfig = {
      itemsAvailable: 5,
      price: 0.05,
      authority: umiInstance.identity.publicKey,
      payer: umiInstance.identity.publicKey,
      candyMachine: machineKeypair,
    };
    
    console.log("Machine configuration:", machineConfig);
    
    const result = await createCandyMachine(umiInstance, machineConfig);
    console.log('Success! Candy machine created:', result);
  } catch (err) {
    console.error('Failed to create candy machine:', err);
  }
}

setupCandyMachineForNFTs();

Anyone know what I’m doing wrong here? Really need help figuring this out!

Had this exact problem building my first NFT drop last month. You’re passing a web3.js Keypair directly to the umi-based candy machine function, but umi needs its own key format. Convert your keypair using umi’s key utilities. Import generateSigner from @metaplex-foundation/umi and swap out your keypair generation: javascript import { generateSigner } from '@metaplex-foundation/umi'; // Replace this line const machineKeypair = generateSigner(umiInstance); Then use candyMachine: machineKeypair in your config (not machineKeypair.publicKey). The generateSigner function creates a umi-compatible signer that works with the candy machine creation process. Also make sure you’re on the latest mpl-candy-machine package - they dropped the old web3.js compatibility layer in recent updates.

u def need just the public key, not the whole keypair. switch that to candyMachine: machineKeypair.publicKey in your setup. also, double check if umiInstance.identity.publicKey is giving u a valid pubkey - sometimes that conversion can mess up!

hmm this looks familiar, had the same headache with candy machines a few weeks ago!

quick question - are you using the latest metaplex packages? you’re mixing @solana/web3.js keypairs with umi, which might be the problem. umi has its own keypair system that doesn’t play nice with regular solana web3 keypairs.

try converting your keypair to umi format first. something like fromWeb3JsKeypair(machineKeypair) should work - just import it from umi.

also what version of mpl-candy-machine are you running? the api changed a lot recently and older tutorials use outdated syntax.

can you share the full error stack trace? sometimes there’s more details buried that might show which exact parameter is causing the “Expected a Public Key” error. could be the authority or payer fields, not just the candyMachine param.