> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/solana-foundation/anchor/llms.txt
> Use this file to discover all available pages before exploring further.

# anchor keys

> Program keypair management commands

## Overview

The `anchor keys` command provides subcommands for managing program keypairs, including listing all program keys and syncing `declare_id!` with actual keypair values.

## Subcommands

* `list` - List all program keys in the workspace
* `sync` - Sync program `declare_id!` with actual keypair pubkeys

***

## anchor keys list

List all program keypairs and their public keys in the workspace.

### Syntax

```bash theme={null}
anchor keys list
```

### Description

This command scans the workspace for program keypairs (typically in `target/deploy/`) and displays:

* Program name
* Program ID (public key)
* Keypair file path

### Example

```bash theme={null}
anchor keys list
```

**Output:**

```
my_program: Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS
my_other_program: 2z9vL1zjN6qyAFHhHQdWYRTFAcy69pJydkZmSFBKHg1R
```

The output shows each program's name and its corresponding program ID. These IDs are derived from the keypairs stored in `target/deploy/`.

### Use Cases

* Verify program IDs before deployment
* Check which programs exist in the workspace
* Copy program IDs for configuration files
* Verify keypair files are present

***

## anchor keys sync

Sync program `declare_id!` pubkeys with the actual pubkeys from program keypair files.

### Syntax

```bash theme={null}
anchor keys sync [OPTIONS]
```

### Options

<ParamField path="--program-name" type="string">
  Only sync the specified program instead of all programs
</ParamField>

### Description

This command:

1. Reads the program ID from the keypair file in `target/deploy/`
2. Updates the `declare_id!` macro in the program's `lib.rs` to match
3. Ensures consistency between keypairs and source code

This is useful when:

* You've generated a new program keypair
* You've copied a program from another workspace
* The `declare_id!` is out of sync with the keypair

### Examples

#### Sync All Programs

```bash theme={null}
anchor keys sync
```

**Output:**

```
Synced program IDs:
  my_program: Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS
  my_other_program: 2z9vL1zjN6qyAFHhHQdWYRTFAcy69pJydkZmSFBKHg1R
```

#### Sync Specific Program

```bash theme={null}
anchor keys sync --program-name my_program
```

**Output:**

```
Synced program ID:
  my_program: Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS
```

### Before and After

**Before sync** (`programs/my_program/src/lib.rs`):

```rust theme={null}
use anchor_lang::prelude::*;

declare_id!("11111111111111111111111111111111");

#[program]
pub mod my_program {
    use super::*;
    // ...
}
```

**After sync**:

```rust theme={null}
use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod my_program {
    use super::*;
    // ...
}
```

## Program Keypairs

### Location

Program keypairs are stored in:

```
target/deploy/<program_name>-keypair.json
```

### Format

Keypair files are JSON arrays containing the secret key bytes:

```json theme={null}
[174,47,154,16,202,...] // 64 bytes total
```

### Generation

Program keypairs are automatically generated during:

* `anchor init` - Creates keypair for initial program
* `anchor new` - Creates keypair for new program
* First build - Creates keypair if missing

## Common Workflows

### Creating a New Program

```bash theme={null}
# Create new program
anchor new my_program

# List keys to see the generated program ID
anchor keys list

# Sync if needed
anchor keys sync --program-name my_program
```

### Using Existing Keypair

```bash theme={null}
# Copy keypair to target/deploy/
cp my-program-keypair.json target/deploy/my_program-keypair.json

# Sync the declare_id! with the keypair
anchor keys sync --program-name my_program

# Verify the sync
anchor keys list
```

### Updating Program Configuration

After syncing, update `Anchor.toml`:

```toml theme={null}
[programs.localnet]
my_program = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[programs.devnet]
my_program = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
```

## Build Integration

By default, `anchor build` checks for program ID mismatches:

```bash theme={null}
# Build with ID verification (default)
anchor build

# Skip ID verification
anchor build --ignore-keys
```

If there's a mismatch, you'll see:

```
Error: Program ID mismatch
  Expected: Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS
  Found:    11111111111111111111111111111111

Run `anchor keys sync` to fix this issue.
```

## Notes

<Warning>
  Keep your program keypairs secure! Anyone with access to the keypair file can upgrade your program (if they're the upgrade authority).
</Warning>

<Tip>
  Run `anchor keys list` before deployment to verify you're deploying the correct program IDs.
</Tip>

<Info>
  The `declare_id!` macro is used at runtime to verify the program is executing under the correct program ID. Keeping it in sync with the keypair is essential.
</Info>

<Note>
  For production deployments, store program keypairs securely and never commit them to version control. Add `target/` to `.gitignore`.
</Note>

## See Also

* [anchor build](/cli/build) - Building programs (includes key verification)
* [anchor deploy](/cli/deploy) - Deploying programs
* [solana-keygen](https://docs.solana.com/cli/usage#solana-keygen) - Solana CLI keypair generation
