An open API service indexing awesome lists of open source software.

https://github.com/iamwavecut/go-tavily

Tavily SDK written in Go (unofficial)
https://github.com/iamwavecut/go-tavily

Last synced: 5 months ago
JSON representation

Tavily SDK written in Go (unofficial)

Awesome Lists containing this project

README

          

# πŸ” Go Tavily Client

[![Go Version](https://img.shields.io/badge/Go-1.24+-00ADD8?style=for-the-badge&logo=go)](https://golang.org/dl/)
[![Go Report Card](https://goreportcard.com/badge/github.com/iamwavecut/go-tavily?style=for-the-badge)](https://goreportcard.com/report/github.com/iamwavecut/go-tavily)
[![GoDoc](https://img.shields.io/badge/pkg.go.dev-reference-007d9c?style=for-the-badge&logo=go)](https://pkg.go.dev/github.com/iamwavecut/go-tavily)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](https://opensource.org/licenses/MIT)
[![Tests](https://img.shields.io/badge/Tests-Passing-brightgreen?style=for-the-badge&logo=github-actions)](https://github.com/iamwavecut/go-tavily/actions)

A modern, flexible Go client for the [Tavily AI-powered search and web content extraction API](https://docs.tavily.com). Built for Go 1.24+ with modern idioms and best practices.

## πŸš€ Features

| Feature | Description | Status |
| ------------------- | ---------------------------------------------------------- | ----------- |
| **Search** | Web search with intelligent results aggregation | βœ… Supported |
| **Extract** | Content extraction from specific URLs | βœ… Supported |
| **Crawl** | Intelligent website crawling and content mapping | βœ… Supported |
| **Map** | Website structure discovery and mapping | βœ… Supported |
| **Context Support** | Full context.Context integration for cancellation/timeouts | βœ… Modern |
| **Error Handling** | Typed errors with semantic checking methods | βœ… Robust |
| **Testing** | Comprehensive test suite | βœ… Complete |
| **Performance** | Built for Go 1.24+ | ⚑ Optimized |

## πŸ“¦ Installation

```bash
go get github.com/iamwavecut/go-tavily
```

## πŸ”§ Quick Start

```go
package main

import (
"context"
"fmt"
"log"

"github.com/iamwavecut/go-tavily"
)

func main() {
// Create client (uses TAVILY_API_KEY env var if empty)
client := tavily.New("tvly-your-api-key", nil)

ctx := context.Background()

// Simple search
result, err := client.SearchSimple(ctx, "Go programming language")
if err != nil {
log.Fatal(err)
}

fmt.Printf("Found %d results in %.2fs\n",
len(result.Results), result.ResponseTime)
}
```

## πŸ“– Usage Examples

### πŸ” Advanced Search

```go
opts := &tavily.SearchOptions{
SearchDepth: string(tavily.SearchDepthAdvanced),
Topic: string(tavily.TopicNews),
MaxResults: 10,
IncludeAnswer: true,
IncludeImages: tavily.BoolPtr(true),
TimeRange: string(tavily.TimeRangeWeek),
IncludeDomains: []string{"github.com", "golang.org"},
Country: "US",
}

result, err := client.Search(ctx, "Go 1.24 release", opts)
```

### 🌐 Content Extraction

```go
urls := []string{
"https://golang.org/doc/",
"https://pkg.go.dev/",
}

opts := &tavily.ExtractOptions{
Format: string(tavily.FormatMarkdown),
ExtractDepth: string(tavily.SearchDepthAdvanced),
IncludeImages: tavily.BoolPtr(true),
}

result, err := client.Extract(ctx, urls, opts)
```

### πŸ•·οΈ Website Crawling

```go
opts := &tavily.CrawlOptions{
MaxDepth: 2,
MaxBreadth: 10,
Limit: 20,
SelectPaths: []string{"/docs/*", "/api/*"},
Categories: []tavily.CrawlCategory{
tavily.CategoryDocumentation,
tavily.CategoryDeveloper,
},
Format: string(tavily.FormatMarkdown),
AllowExternal: tavily.BoolPtr(false),
}

result, err := client.Crawl(ctx, "https://docs.tavily.com", opts)
```

### πŸ—ΊοΈ Website Mapping

```go
opts := &tavily.MapOptions{
MaxDepth: 3,
Limit: 100,
Categories: []tavily.CrawlCategory{
tavily.CategoryDocumentation,
tavily.CategoryBlog,
},
SelectPaths: []string{"/docs/*", "/blog/*"},
}

result, err := client.Map(ctx, "https://docs.tavily.com", opts)
```

## 🎯 Convenience Methods

| Method | Purpose | Example |
| ---------------------- | ------------------------------------ | ---------------- |
| `SearchSimple()` | Basic search with minimal config | Quick searches |
| `SearchWithAnswer()` | Search with AI-generated answer | Q&A applications |
| `SearchNews()` | News-focused search with time filter | Recent updates |
| `ExtractSimple()` | Single URL extraction | Content analysis |
| `ExtractWithImages()` | Multi-URL extraction with images | Rich content |
| `CrawlDocumentation()` | Documentation-focused crawling | API docs, guides |
| `MapSite()` | Quick website structure mapping | Site analysis |
| `GetSearchContext()` | RAG-formatted search results | AI applications |

## πŸ› οΈ Configuration

### Client Options

```go
opts := &tavily.Options{
BaseURL: "https://api.tavily.com", // Custom API endpoint
HTTPClient: customHTTPClient, // Custom HTTP client
Timeout: 45 * time.Second, // Request timeout
}

client := tavily.New("your-api-key", opts)
```

### Custom HTTP Client

```go
customClient := &http.Client{
Timeout: 45 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
},
}

client := tavily.New("your-api-key", &tavily.Options{
HTTPClient: customClient,
})
```

## 🚨 Error Handling

The client provides semantic error checking methods:

```go
result, err := client.Search(ctx, "query", nil)
if err != nil {
if apiErr, ok := err.(*tavily.APIError); ok {
switch {
case apiErr.IsUnauthorized():
fmt.Println("Invalid API key")
case apiErr.IsRateLimit():
fmt.Println("Rate limit exceeded")
case apiErr.IsForbidden():
fmt.Println("Access forbidden")
case apiErr.IsBadRequest():
fmt.Println("Invalid parameters")
default:
fmt.Printf("API error: %s\n", apiErr.Message)
}
} else {
fmt.Printf("Other error: %v\n", err)
}
}
```

## πŸ§ͺ Testing

The client includes comprehensive tests:

```bash
# Run all tests
go test -v ./...

# Run benchmarks with new testing.B.Loop
go test -bench=. -count=3

# Test with coverage
go test -cover ./...
```

## πŸƒβ€β™‚οΈ Demo Application

```bash
# Set your API key
export TAVILY_API_KEY="tvly-your-api-key"

# Run the demo
go run cmd/demo/main.go
```

## πŸ”§ Environment Variables

| Variable | Description | Required |
| -------------------- | ------------------- | ---------- |
| `TAVILY_API_KEY` | Your Tavily API key | βœ… Yes |
| `TAVILY_HTTP_PROXY` | HTTP proxy URL | ❌ Optional |
| `TAVILY_HTTPS_PROXY` | HTTPS proxy URL | ❌ Optional |

## πŸ“‹ API Coverage

| Endpoint | Method | Status | Features |
| ---------- | ----------- | ---------- | ----------------------------------------- |
| `/search` | `Search()` | βœ… Complete | All parameters, answer generation, images |
| `/extract` | `Extract()` | βœ… Complete | Multi-URL, formats, depth control |
| `/crawl` | `Crawl()` | βœ… Complete | Path filtering, categories, depth limits |
| `/map` | `Map()` | βœ… Complete | Structure discovery, URL filtering |

## 🀝 Contributing

1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure all tests pass: `go test -v ./...`
5. Submit a pull request

## πŸ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## πŸ”— Related Links

- [Tavily API Documentation](https://docs.tavily.com)