How to export a private key from Phantom to a Keypair file

How to export a private key from Phantom to a Keypair file

Did you create a wallet directly from Phantom and now you want to use it in the Solana CLI? This simple tutorial explains how to do that.


1 min read

Export from Phantom

  • Go to Settings and then click Export Private Key

  • Enter your password

  • Copy the base58 private key

phantom export private key

Generating the Keypair file

Change the PRIVATE_KEY and PUBLIC_KEY values to the ones you want to export.
You can also set PRIVATE_KEY as an environment variable. Live code here

import { Keypair } from '@solana/web3.js';
import base58 from "bs58";
import * as fs from 'fs';

const PRIVATE_KEY = ""; // Private key from phantom
const PUBLIC_KEY = ""; // Fill with your address to verify
const secret = base58.decode(PRIVATE_KEY);

// Check if the pk is correct 
const pair = Keypair.fromSecretKey(secret);

if (pair.publicKey.toString() == PUBLIC_KEY) {

Setting the Keypair file to Solana CLI

Once the file is created you can set the Keypair path with this command

solana config set -k path/to/keypair/private_key.json

solana config keypair path command

All set!

To check if the file was set correctly you can use solana address it will show your wallet address.

Happy Coding!