Skip to content

lnbotdev/go-sdk

Repository files navigation

ln.bot-go

Go Reference License: MIT

The official Go SDK for ln.bot — Bitcoin for AI Agents.

Give your AI agents, apps, and services access to Bitcoin over the Lightning Network. Create wallets, send and receive sats, and get real-time payment notifications.

client := lnbot.New("key_...")

invoice, _ := client.Invoices.Create(ctx, &lnbot.CreateInvoiceParams{
    Amount: 1000,
    Memo:   lnbot.Ptr("Coffee"),
})

ln.bot also ships a TypeScript SDK, Python SDK, Rust SDK, CLI, and MCP server.


Install

go get github.com/lnbotdev/go-sdk

Quick start

Create a wallet

package main

import (
    "context"
    "fmt"

    lnbot "github.com/lnbotdev/go-sdk"
)

func main() {
    client := lnbot.New("")
    wallet, _ := client.Wallets.Create(context.Background(), &lnbot.CreateWalletParams{
        Name: lnbot.Ptr("my-agent"),
    })
    fmt.Println(wallet.PrimaryKey)
}

Receive sats

client := lnbot.New(wallet.PrimaryKey)

invoice, _ := client.Invoices.Create(ctx, &lnbot.CreateInvoiceParams{
    Amount: 1000,
    Memo:   lnbot.Ptr("Payment for task #42"),
})
fmt.Println(invoice.Bolt11)

Wait for payment (SSE)

events, errs := client.Invoices.Watch(ctx, invoice.Number, nil)
for event := range events {
    if event.Event == "settled" {
        fmt.Println("Paid!")
    }
}
if err := <-errs; err != nil {
    log.Fatal(err)
}

Send sats

payment, _ := client.Payments.Create(ctx, &lnbot.CreatePaymentParams{
    Target: "alice@ln.bot",
    Amount: lnbot.Ptr(int64(500)),
})

Check balance

wallet, _ := client.Wallets.Current(ctx)
fmt.Printf("%d sats available\n", wallet.Available)

L402 paywalls

// Create a challenge (server side)
challenge, _ := client.L402.CreateChallenge(ctx, &lnbot.CreateL402ChallengeParams{
    Amount:      100,
    Description: lnbot.Ptr("API access"),
})

// Pay the challenge (client side)
result, _ := client.L402.Pay(ctx, &lnbot.PayL402Params{
    WwwAuthenticate: challenge.WwwAuthenticate,
})

// Verify a token (server side, stateless)
v, _ := client.L402.Verify(ctx, &lnbot.VerifyL402Params{
    Authorization: *result.Authorization,
})
fmt.Println(v.Valid)

Error handling

import "errors"

wallet, err := client.Wallets.Current(ctx)
if err != nil {
    // Match any API error
    var apiErr *lnbot.APIError
    if errors.As(err, &apiErr) {
        fmt.Println(apiErr.StatusCode, apiErr.Message)
    }

    // Match specific error types
    var notFound *lnbot.NotFoundError
    var badReq *lnbot.BadRequestError
    var conflict *lnbot.ConflictError

    switch {
    case errors.As(err, &notFound):
        // 404
    case errors.As(err, &badReq):
        // 400
    case errors.As(err, &conflict):
        // 409
    default:
        // other error
    }
}

Configuration

client := lnbot.New("key_...",
    lnbot.WithBaseURL("https://api.ln.bot"),
    lnbot.WithHTTPClient(customHTTPClient),
)

Features

  • Zero dependencies — stdlib net/http + encoding/json only
  • Context-first — every method takes context.Context as the first argument
  • Typed errorsBadRequestError, NotFoundError, ConflictError, UnauthorizedError, ForbiddenError
  • Generic helpersPtr[T] for optional fields
  • SSE supportWatch returns channels for real-time events

Requirements

  • Go 1.21+
  • Get your API key at ln.bot

Links

Other SDKs

License

MIT

About

The official Go SDK for ln.bot - Bitcoin for AI Agents.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages