> ## Documentation Index
> Fetch the complete documentation index at: https://second.tech/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Get started with Rust

> Add Ark payments to your Rust application. Create a wallet, generate addresses, and check your balance in minutes.

The `bark-wallet` crate gives you direct access to the core Bark wallet library.

<Prompt description="Use this prompt to get started quickly." icon="microchip" iconType="solid" actions={["copy", "cursor"]}>
  # Bark Rust SDK

  Build Ark wallet apps with the `bark-wallet` Rust crate. Ark is a Bitcoin layer 2 protocol for instant, low-fee, self-custodial payments.

  ## Prerequisites

  * Rust 1.77+ with Cargo
  * Tokio async runtime (1.35+)

  ## Install

  ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
  cargo add bark-wallet
  cargo add tokio --features full
  ```

  ## Create a wallet

  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  use std::path::PathBuf;
  use std::sync::Arc;
  use tokio::fs;
  use bark::{Config, onchain, Wallet};
  use bark::persist::sqlite::SqliteClient;

  const MNEMONIC_FILE : &str = "mnemonic";
  const DB_FILE: &str = "db.sqlite";

  #[tokio::main]
  async fn main() {
  	// Pick the bitcoin network that will be used
  	let network = bitcoin::Network::Signet;

  	// Configure the wallet
  	let config = Config {
  		server_address: String::from("https://ark.signet.2nd.dev"),
  		esplora_address: Some(String::from("https://esplora.signet.2nd.dev")),
  		..Config::network_default(network)
  	};


  	// Create a sqlite database
  	let datadir = PathBuf::from("./bark");
  	let db = Arc::new(SqliteClient::open(datadir.join(DB_FILE)).unwrap());

  	// Generate and seed and store it somewhere
  	let mnemonic = bip39::Mnemonic::generate(12).expect("12 is valid");
  	fs::write(datadir.join(MNEMONIC_FILE), mnemonic.to_string().as_bytes()).await.unwrap();

  	let wallet = Wallet::create(
  		&mnemonic,
  		network,
  		config,
  		db,
  		false
  	).await.unwrap();
  }
  ```

  ## Get address and check balance

  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  let address = wallet.new_address().await?;
  wallet.sync().await?;
  let balance = wallet.balance().await?;
  println!("Spendable: {} sats", balance.spendable.to_sat());
  ```

  ## Signet test environment

  * Ark server: [https://ark.signet.2nd.dev](https://ark.signet.2nd.dev)
  * Block explorer (Esplora): [https://esplora.signet.2nd.dev](https://esplora.signet.2nd.dev)
  * Faucet for test bitcoin: [https://signet.2nd.dev/](https://signet.2nd.dev/)
  * Network enum: `Network::Signet`

  ## Rules

  * All wallet operations are async. Use Tokio and `.await`.
  * Always call `wallet.sync().await?` before `wallet.balance().await?`.
  * Use `Arc<SqliteClient>` for the database parameter.
  * Import from `bark::Wallet`, `bark::config::Config`, `bark::persist::sqlite::SqliteClient`.
  * Use `Network::Signet` for testing. Never use mainnet without user confirmation.
  * Never hardcode mnemonics in source code.
  * The crate name is `bark-wallet`, not `bark-bitcoin` or `bark-sdk`.
  * Correct terminology: Ark (not ARC), VTXO (not vtxo), on-chain (not onchain), off-chain (not offchain).
  * "Bark" (capitalized) in prose, "bark" (lowercase) for the crate name and CLI commands.
</Prompt>

## Prerequisites

* Rust 1.77+ with Cargo
* Tokio async runtime (1.35+)

## Getting started

<Steps>
  <Step title="Add the dependency">
    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    cargo add bark-wallet
    ```

    You will also need Tokio as the async runtime:

    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    cargo add tokio --features full
    ```
  </Step>

  <Step title="Create a wallet">
    Generate a BIP39 mnemonic, configure the wallet to use the signet test network, and initialize it.

    ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
    use std::path::PathBuf;
    use std::sync::Arc;
    use tokio::fs;
    use bark::{Config, onchain, Wallet};
    use bark::persist::sqlite::SqliteClient;

    const MNEMONIC_FILE : &str = "mnemonic";
    const DB_FILE: &str = "db.sqlite";

    #[tokio::main]
    async fn main() {
      // Pick the bitcoin network that will be used
      let network = bitcoin::Network::Signet;

      // Configure the wallet
      let config = Config {
        server_address: String::from("https://ark.signet.2nd.dev"),
        esplora_address: Some(String::from("https://esplora.signet.2nd.dev")),
        ..Config::network_default(network)
      };

      // Create a sqlite database
      let datadir = PathBuf::from("./bark");
      let db = Arc::new(SqliteClient::open(datadir.join(DB_FILE)).unwrap());

      // Generate a seed phrase and store it somewhere
      let mnemonic = bip39::Mnemonic::generate(12).expect("12 is valid");
      fs::write(datadir.join(MNEMONIC_FILE), mnemonic.to_string().as_bytes()).await.unwrap();

      let wallet = Wallet::create(
        &mnemonic,
        network,
        config,
        db,
        false
      ).await.unwrap();
    }
    ```

    <Note>
      Back up the mnemonic securely. It is the only way to recover your wallet.
    </Note>
  </Step>

  <Step title="Get a receiving address">
    Generate an Ark address to receive funds.

    ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
    let address = wallet.new_address().await?;
    println!("Ark address: {}", address);
    ```

    Send some signet sats to this address using [the faucet](https://signet.2nd.dev/).
  </Step>

  <Step title="Check your balance">
    Sync the wallet with the Ark server and read your balance.

    ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
    wallet.sync().await?;

    let balance = wallet.balance().await?;
    println!("Spendable: {} sats", balance.spendable.to_sat());
    ```
  </Step>
</Steps>

## Next steps

<CardGroup cols={2}>
  <Card title="API reference" icon="book" href="https://docs.rs/bark-wallet/latest/bark/index.html">
    Full Rust API documentation on docs.rs.
  </Card>

  <Card title="How Ark works" icon="graduation-cap" href="/learn/intro">
    Learn about the protocol powering your wallet.
  </Card>
</CardGroup>
