Skip to main content

Quick Start

This guide will walk you through creating your first Leo program. You’ll learn how to create a new project, understand the project structure, write Leo code, and run your program.
Make sure you have installed Leo before continuing with this guide.

Create Your First Program

1

Create a new Leo project

Use the Leo CLI to create a new project:
leo new helloworld
This creates a new directory called helloworld with a complete Leo project structure.
2

Navigate to the project directory

cd helloworld
3

Explore the project structure

Your new Leo project has the following structure:
helloworld/
├── src/
│   └── main.leo          # Your program source code
├── program.json          # Program configuration
└── README.md            # Project documentation

Understanding the Default Program

Let’s look at the default program created in src/main.leo:
program helloworld.aleo {
    // The 'helloworld' main function.
    fn main(public a: u32, b: u32) -> u32 {
        return a + b;
    }
}

Program Structure

  • Program declaration: program helloworld.aleo defines the program name
  • Function: fn main is the entry point of the program
  • Parameters: a and b are inputs (both public u32 unsigned 32-bit integers)
  • Return type: -> u32 specifies the function returns a 32-bit unsigned integer
  • Logic: The function simply adds the two inputs and returns the result
All Leo programs must have the .aleo suffix in their program declaration.

Run Your Program

Leo makes it easy to run your program with a single command:
leo run main 0u32 1u32
This command:
  1. Compiles the program into Aleo instructions
  2. Executes the main function with inputs 0u32 and 1u32
  3. Displays the output
Leo Compiled 'helloworld.aleo' into Aleo instructions

  Constraints
 - 'helloworld.aleo/main' - 0 constraints (0 public, 0 private)

➡️  Output
 - 1u32

 Executed 'helloworld.aleo/main'
The leo run command automatically builds your program before running it, so you don’t need to run leo build separately.

Try Different Inputs

Let’s experiment with different inputs:
leo run main 42u32 18u32
The output should be 60u32.

Build Your Own Program

Now let’s modify the program to do something more interesting.
1

Create a Fibonacci function

Edit src/main.leo to implement a Fibonacci calculator:
program helloworld.aleo {
    // Calculate fibonacci number at position n
    fn fibonacci(public n: u8) -> u128 {
        assert(n <= 64u8);

        let f0: u128 = 0u128;
        let f1: u128 = 1u128;
        let c: u8 = 0u8;

        let z: u8 = reverse_bits(n);

        for i:u8 in 0u8..8u8 {
            if n > 0u8 {
                let f2i1: u128 = f1 * f1 + f0 * f0;
                let f2i: u128 = f0 * (2u128 * f1 - f0);
                if z & 1u8.shl(c) == 0u8 {
                    f0 = f2i;
                    f1 = f2i1;
                } else {
                    f0 = f2i1;
                    f1 = f2i + f2i1;
                }
                c = c + 1u8;
                n = n >> 1u8;
            }
        }

        return f0;
    }
}

fn reverse_bits(n: u8) -> u8 {
    let reverse: u8 = 0u8;

    for i:u8 in 0u8..8u8 {
        if n > 0u8 {
            reverse = reverse << 1u8;

            if n & 1u8 == 1u8 {
                reverse ^= 1u8;
            }

            n = n >> 1u8;
        }
    }

    return reverse;
}
2

Run the Fibonacci program

leo run fibonacci 10u8
This calculates the 10th Fibonacci number.

Understanding Leo Commands

Here are the essential Leo CLI commands:
leo new <PROJECT_NAME>

Working with Different Data Types

Leo supports various data types. Here’s a more comprehensive example:
program datatypes.aleo {
    record Token {
        owner: address,
        amount: u64,
    }

    // Create a new token record
    fn mint(receiver: address, amount: u64) -> Token {
        return Token {
            owner: receiver,
            amount: amount,
        };
    }

    // Transfer tokens
    fn transfer(sender: Token, receiver: address, amount: u64) -> (Token, Token) {
        let difference: u64 = sender.amount - amount;

        let remaining: Token = Token {
            owner: sender.owner,
            amount: difference,
        };

        let transferred: Token = Token {
            owner: receiver,
            amount: amount,
        };

        return (remaining, transferred);
    }
}

Key Data Types

  • Integers: u8, u16, u32, u64, u128, i8, i16, i32, i64, i128
  • Boolean: bool
  • Field elements: field
  • Group elements: group
  • Address: address
  • Records: Custom data structures that represent program state
Records in Leo are special data types that represent ownership and can be used to manage state privately on the Aleo blockchain.

Network Configuration

When creating a new project, you can specify the network:
leo new myproject --network testnet --endpoint https://api.explorer.provable.com/v1
Available networks:
  • testnet (default)
  • mainnet
  • canary

Common Command Options

Run with specific network

leo run main 5u32 10u32 --network testnet

Build with offline mode

leo build --offline

Run with debug output

leo run main 1u32 2u32 --debug

Quiet mode (suppress output)

leo run main 1u32 2u32 --quiet

Next Steps

Congratulations! You’ve just run your first Leo program. Here’s what to explore next:

Language Reference

Learn Leo syntax and language features

Example Programs

Explore more complex Leo programs

Deploy Programs

Learn how to deploy to the Aleo network

Testing Guide

Write tests for your Leo programs

Troubleshooting

Program won’t compile

Make sure:
  • Your program name matches the directory name
  • All functions have proper type annotations
  • Syntax is correct (check semicolons, braces, etc.)

”Failed to execute” errors

Common causes:
  • Incorrect input types (e.g., using 1 instead of 1u32)
  • Wrong number of inputs
  • Input values that cause assertion failures

Network connection issues

If you’re having trouble connecting to the network:
leo run main 1u32 2u32 --endpoint http://localhost:3030
This uses a local endpoint instead of the default remote endpoint.