# Elixir client for Turso Cloud Platform API

[](https://hex.pm/packages/turso)
[](https://hexdocs.pm/turso/)
[](https://github.com/vitalis/turso/blob/main/LICENSE)
[](https://github.com/vitalis/turso/actions)
[](https://coveralls.io/github/vitalis/turso?branch=main)
[](https://github.com/vitalis/turso/commits/main)
---
Elixir client for [Turso Cloud Platform API](https://turso.tech), providing access to distributed SQLite databases.
- โ
Complete [Turso Cloud Platform API](https://docs.turso.tech/api-reference) implementation
- ๐ Multi-region database support with edge replication
- โก Configurable HTTP client with retry strategies
- ๐ก๏ธ Comprehensive error handling with helper functions
- ๐งช Built-in mock server for testing
- ๐ Streaming support for large datasets
- ๐ง Full type specifications with Dialyzer support
## Installation
The package can be installed by adding `turso` to your list of dependencies in `mix.exs`.
```elixir
def deps do
[
{:turso, "~> 0.1.0"}
]
end
```
## Quickstart
### Configuration
By default, HTTP retries are disabled for better control. You can configure the underlying HTTP client behavior:
```elixir
# config/config.exs
config :turso, :req_options,
retry: :safe_transient, # Enable retries for safe operations
max_retries: 3,
receive_timeout: 60_000
```
For more examples, refer to the [Turso documentation](https://hexdocs.pm/turso).
### Initialize a client
See `Turso.init/2`.
```elixir
client = Turso.init("your-api-token")
```
### Manage databases
See `Turso.Databases`.
```elixir
# List databases
{:ok, databases} = Turso.list_databases(client)
# Create a database
{:ok, database} = Turso.create_database(client, "my-app-db", [
group: "production",
size_limit: "1gb"
])
# Create database connection token
{:ok, token} = Turso.create_database_token(client, "my-app-db", [
expiration: "30d",
authorization: "full-access"
])
```
### Working with groups
See `Turso.Groups`.
```elixir
# Create a group in multiple regions
{:ok, group} = Turso.create_group(client, "global", location: "iad")
{:ok, group} = Turso.add_location(client, "global", "lhr")
{:ok, group} = Turso.add_location(client, "global", "nrt")
# Ensure group exists (create if needed)
{:ok, :exists} = Turso.ensure_group_exists(client, "staging")
```
### Error handling
The library provides comprehensive error handling with helper functions:
```elixir
case Turso.create_database(client, "existing-db") do
{:ok, database} ->
IO.puts("Database created!")
{:error, %Turso.Error{} = error} ->
cond do
Turso.Error.rate_limited?(error) ->
retry_after = Turso.Error.retry_after(error)
IO.puts("Rate limited, retry after #{retry_after}s")
Turso.Error.auth_error?(error) ->
IO.puts("Authentication failed")
Turso.Error.retryable?(error) ->
IO.puts("Retryable error occurred")
true ->
IO.puts("Error: #{error.message}")
end
end
```
### Streaming
Stream large datasets efficiently:
```elixir
# Stream all audit logs with automatic pagination
client
|> Turso.stream_audit_logs(action: "database.delete")
|> Stream.filter(&recent?/1)
|> Enum.take(100)
```
### Testing
Use the built-in mock server for testing:
```elixir
test "database operations" do
client = Turso.Mock.client(&Turso.Mock.respond(&1, :database_list))
assert {:ok, databases} = Turso.list_databases(client)
assert is_list(databases)
end
```
## License
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.
## Links
- [Turso Platform](https://turso.tech)
- [API Documentation](https://docs.turso.tech/api-reference)
- [Full Documentation](https://hexdocs.pm/turso)