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

# Go SDK

> Official Go client library for the Rusty Browser API

## Installation

```bash theme={null}
go get github.com/wraithbytes/sdk-go
```

Requires Go 1.21+.

## Quick Start

```go theme={null}
package main

import (
    "context"
    "fmt"
    "log"

    rustybrowser "github.com/wraithbytes/sdk-go"
)

func main() {
    client := rustybrowser.NewClient("")

    // Create a browser
    browser, err := client.CreateBrowser(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    defer client.DeleteBrowser(context.Background(), browser.ID)

    // Navigate to a URL
    if err := client.Navigate(context.Background(), browser.ID, "https://example.com"); err != nil {
        log.Fatal(err)
    }

    // Fetch page text
    text, err := client.FetchText(context.Background(), browser.ID)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(text)
}
```

## Client

### `NewClient(apiKey string) *Client`

Creates a new API client. Pass an empty string when your self-hosted server does not enforce `X-API-Key`; pass your local server key if you enabled API-key authentication.

```go theme={null}
client := rustybrowser.NewClient("")
```

## Methods

### Browser Management

#### `CreateBrowser(ctx) (*Browser, error)`

Spawn a new browser instance.

```go theme={null}
browser, err := client.CreateBrowser(ctx)
fmt.Println(browser.ID) // UUID
```

#### `GetBrowser(ctx, id) (*Browser, error)`

Get the current state of a browser.

```go theme={null}
browser, err := client.GetBrowser(ctx, "550e8400-e29b-41d4-a716-446655440000")
```

#### `DeleteBrowser(ctx, id) error`

Close a browser and release its resources.

```go theme={null}
err := client.DeleteBrowser(ctx, "550e8400-e29b-41d4-a716-446655440000")
```

### Commands

#### `Navigate(ctx, id, url) error`

Navigate to a URL.

```go theme={null}
err := client.Navigate(ctx, browser.ID, "https://example.com")
```

#### `Click(ctx, id, selector) error`

Click an element.

```go theme={null}
err := client.Click(ctx, browser.ID, "#submit-button")
```

#### `NodeClick(ctx, id, node) error`

Click by node reference.

```go theme={null}
err := client.NodeClick(ctx, browser.ID, nodeRef)
```

#### `Type(ctx, id, req) error`

Type text into a field.

```go theme={null}
err := client.Type(ctx, browser.ID, &rustybrowser.TypeRequest{
    Selector: "#search-input",
    Text:     "rusty browser",
})
```

#### `ScrollBy(ctx, id, x, y) error`

Scroll by a relative amount.

```go theme={null}
err := client.ScrollBy(ctx, browser.ID, 0, 500)
```

#### `ScrollTo(ctx, id, req) error`

Scroll to an element or position.

```go theme={null}
err := client.ScrollTo(ctx, browser.ID, &rustybrowser.ScrollToRequest{
    Selector: "#footer",
})
```

#### `Screenshot(ctx, id) (*Screenshot, error)`

Capture a screenshot.

```go theme={null}
shot, err := client.Screenshot(ctx, browser.ID)
```

#### `FetchHTML(ctx, id) (string, error)`

Fetch the current page HTML.

```go theme={null}
html, err := client.FetchHTML(ctx, browser.ID)
```

#### `FetchText(ctx, id) (string, error)`

Fetch the current page text.

```go theme={null}
text, err := client.FetchText(ctx, browser.ID)
```

#### `Eval(ctx, id, expression) (interface{}, error)`

Evaluate a JavaScript expression.

```go theme={null}
result, err := client.Eval(ctx, browser.ID, "document.title")
```

#### `Instruct(ctx, id, instruction) (*InstructResult, error)`

Issue a natural language instruction.

```go theme={null}
result, err := client.Instruct(ctx, browser.ID, "Find and return all product prices on this page")
```

## Types

### Browser

```go theme={null}
type Browser struct {
    ID string `json:"id"`
}
```

### TypeRequest

```go theme={null}
type TypeRequest struct {
    Text     string `json:"text"`
    Selector string `json:"selector,omitempty"`
}
```

### ScrollToRequest

```go theme={null}
type ScrollToRequest struct {
    Selector string  `json:"selector,omitempty"`
    X        float64 `json:"x,omitempty"`
    Y        float64 `json:"y,omitempty"`
}
```

### ErrorDetail

```go theme={null}
type ErrorDetail struct {
    Code    string `json:"code"`
    Message string `json:"message"`
}
```
