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)
- Host: GitHub
- URL: https://github.com/iamwavecut/go-tavily
- Owner: iamwavecut
- License: mit
- Created: 2025-06-18T19:40:18.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-18T20:44:41.000Z (12 months ago)
- Last Synced: 2025-09-20T10:35:09.470Z (9 months ago)
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 8
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π Go Tavily Client
[](https://golang.org/dl/)
[](https://goreportcard.com/report/github.com/iamwavecut/go-tavily)
[](https://pkg.go.dev/github.com/iamwavecut/go-tavily)
[](https://opensource.org/licenses/MIT)
[](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)