# Go server SDK

> Idiomatic Go client for creating P2P calls and SFU rooms from any Go service.

```sh
go get github.com/radist-tech/go-server
```

## Constructor

`radist.NewClient(secretToken, ...opts)` returns a configured client.

| Option                           | Description                                               |
| -------------------------------- | --------------------------------------------------------- |
| `WithProjectID(id string)`       | Set the project id used for all requests.                 |
| `WithAPIBaseURL(url string)`     | Override the default API base URL.                        |
| `WithHTTPClient(c *http.Client)` | Provide a custom HTTP client with timeouts or middleware. |

## API surface

| Method                                                       | Returns                           | Description                                                                          |
| ------------------------------------------------------------ | --------------------------------- | ------------------------------------------------------------------------------------ |
| `CreateP2PConnection(ctx, opts)`                             | `P2PConnection, error`            | Creates a two-peer call and returns two participant tokens. Pass `nil` for defaults. |
| `CreateRoom(ctx, opts)`                                      | `Room, error`                     | Creates an SFU room and returns two room tokens.                                     |
| `MintConnectionToken(ctx, opts)`                             | `ConnectionToken, error`          | Mints an extra participant token for an existing call or room.                       |
| `CreateSpace(ctx, opts)`                                     | `SpaceCreated, error`             | Creates a persistent space and returns participant and one-time host links.          |
| `ListSpaces(ctx, opts)` / `GetSpace(ctx, id, opts)`          | `[]Space, error` / `Space, error` | Reads persistent space definitions.                                                  |
| `UpdateSpace()` / `DeleteSpace()` / `RotateSpaceHostToken()` | —                                 | Updates admission settings, deletes a definition, or replaces its host link.         |

## Create a P2P call

```go
package main

import (
	"context"
	"fmt"
	"log"

	radist "github.com/radist-tech/go-server"
)

func main() {
	client, err := radist.NewClient("rad_sk_your_secret_key",
		radist.WithProjectID("your_project_id"),
	)
	if err != nil {
		log.Fatal(err)
	}

	// Pass nil to use all defaults (access control defaults to "private").
	conn, err := client.CreateP2PConnection(context.Background(), nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("call id:", conn.CallID)
	fmt.Println("tokens:", conn.CallTokens)
}
```

## Access control and extra tokens

```go
// Public — anyone with your public key can join:
conn, err := client.CreateP2PConnection(ctx, &radist.CreateP2PConnectionOptions{
	AccessControl: "public",
})

// For private calls, mint additional tokens as needed:
token, err := client.MintConnectionToken(ctx, radist.MintConnectionTokenOptions{
	CallID: conn.CallID,
})
```

## Rooms and spaces

```go
room, err := client.CreateRoom(context.Background(), radist.CreateRoomOptions{})

space, err := client.CreateSpace(context.Background(), radist.CreateSpaceOptions{
	Name:       "Daily standup",
	AccessType: radist.SpaceAccessPassword,
	Password:   "shared-secret",
})
```

## Error handling

API errors are returned as `*radist.APIError` with `Status int` and `Message string`.

```go
var apiErr *radist.APIError
if errors.As(err, &apiErr) {
	fmt.Printf("API error %d: %s\n", apiErr.Status, apiErr.Message)
}
```
