Skip to content

Examples

Complete examples from the examples directory.

Hello World

haira
import "io"

fn main() {
    io.println("Hello, World!")
}

Simple Chatbot

A streaming chatbot with memory:

haira
import "io"
import "http"

provider openai {
    api_key: env("OPENAI_API_KEY")
    model: "gpt-4o"
}

agent Writer {
    provider: openai
    system: "You are a creative writer."
    memory: conversation(max_turns: 10)
    temperature: 0.9
}

@post("/chat")
workflow Chat(msg: string, sid: string) -> stream {
    return Writer.stream(msg, session: sid)
}

fn main() {
    http.Server([Chat]).listen(8080)
}

Weather Agent

Agent with a tool that fetches live weather data:

haira
import "io"
import "http"

provider openai {
    api_key: env("OPENAI_API_KEY")
    model: "gpt-4o"
}

tool get_weather(city: string) -> string {
    """Get the current weather for a given city"""
    resp, err = http.get("https://wttr.in/${city}?format=j1")
    if err != nil { return "Failed to fetch weather data." }
    data = resp.json()
    current = data["current_condition"][0]
    temp_c = current["temp_C"]
    desc = current["weatherDesc"][0]["value"]
    return "${city}: ${temp_c}°C, ${desc}"
}

agent Assistant {
    provider: openai
    system: "You are a helpful assistant. Be concise."
    tools: [get_weather]
    memory: conversation(max_turns: 10)
    temperature: 0.7
}

@webhook("/api/chat")
workflow Chat(message: string, session_id: string) -> { reply: string } {
    reply, err = Assistant.ask(message, session: session_id)
    if err != nil { return { reply: "Something went wrong." } }
    return { reply: reply }
}

fn main() {
    server = http.Server([Chat])
    io.println("Server running on :9001")
    server.listen(9001)
}

Multi-Agent Support Desk

Front desk routing to specialized agents:

haira
import "io"
import "http"

provider openai {
    api_key: env("OPENAI_API_KEY")
    model: "gpt-4o"
}

agent BillingAgent {
    provider: openai
    system: "Handle billing and payment questions."
    memory: conversation(max_turns: 20)
}

agent TechAgent {
    provider: openai
    system: "Handle technical support questions."
    memory: conversation(max_turns: 20)
}

agent FrontDesk {
    provider: openai
    system: "Greet users. Route billing to BillingAgent, tech to TechAgent."
    handoffs: [BillingAgent, TechAgent]
    memory: conversation(max_turns: 10)
    temperature: 0.3
}

@post("/support")
workflow Support(msg: string, sid: string) -> { reply: string } {
    reply, err = FrontDesk.ask(msg, session: sid)
    if err != nil { return { reply: "Something went wrong." } }
    return { reply: reply }
}

fn main() {
    http.Server([Support]).listen(8080)
}

File Upload & Summarize

haira
import "io"
import "http"

provider openai {
    api_key: env("OPENAI_API_KEY")
    model: "gpt-4o"
}

agent Summarizer {
    provider: openai
    system: "Summarize the given text clearly and concisely."
    temperature: 0.3
}

@webui(title: "File Summarizer", description: "Upload a text file and get an AI summary")
@post("/summarize")
workflow Summarize(document: file, context: string) -> { summary: string } {
    content, err = io.read_file(document)
    if err != nil { return { summary: "Failed to read file." } }

    prompt = "Summarize this: ${context}\n\n${content}"
    reply, err = Summarizer.ask(prompt)
    if err != nil { return { summary: "AI error." } }
    return { summary: reply }
}

fn main() {
    http.Server([Summarize]).listen(8080)
}

Pipe Operator

haira
import "io"
import "string"

fn double(x: int) -> int { return x * 2 }
fn add_ten(x: int) -> int { return x + 10 }

fn main() {
    result = 5 |> double |> add_ten
    io.println("Result: ${result}")  // 20

    text = "  Hello World  "
        |> string.trim
        |> string.to_lower
    io.println(text)  // "hello world"
}

Pattern Matching

haira
import "io"

enum Shape {
    Circle(float)
    Rectangle(float, float)
    Point
}

fn area(s: Shape) -> float {
    match s {
        Shape.Circle(r) => 3.14159 * r * r,
        Shape.Rectangle(w, h) => w * h,
        Shape.Point => 0.0
    }
}

fn main() {
    shapes = [
        Shape.Circle(5.0),
        Shape.Rectangle(3.0, 4.0),
        Shape.Point
    ]

    for shape in shapes {
        io.println("Area: ${area(shape)}")
    }
}

More Examples

See the full examples directory on GitHub with 35 examples covering:

#ExampleDescription
01Hello WorldBasic program
02VariablesType inference and assignment
03FunctionsFunction definitions
04Control FlowIf/else, for loops
05MatchPattern matching
06ListsList operations
07AgenticProviders, tools, agents
08StructsStruct definitions and use
09String Interpolation${expr} syntax
10MapsMap operations
11PipesPipe operator
12MethodsType methods with self
13Error HandlingError patterns
14Multi-AgentMultiple agents
15HandoffsAgent routing
16EnumsEnum definitions
17Compound Assignment+=, -=, etc.
18DeferCleanup with defer
19StreamingSSE streaming
20StdlibStandard library usage
21File UploadFile handling
22Pipeline UIStep-based workflows
23MCP ClientMCP protocol
24MCP ServerMCP server
25EmbeddingsVector embeddings
26RAGRetrieval augmented generation
27Structured OutputTyped agent responses
28ObserveLLM observability
29TestingTest framework
30Type AliasesType alias definitions
31SpawnParallel spawn blocks
32BitwiseBitwise operators
33Lifecycle HooksWorkflow lifecycle hooks
34Dynamic AgentsRuntime agent creation
35Stdlib ToolsPre-built agent templates

Released under the Apache-2.0 License.