package main
import (
"context"
"fmt"
"io"
"os"
reducto "github.com/reductoai/reducto-go-sdk"
"github.com/reductoai/reducto-go-sdk/option"
"github.com/reductoai/reducto-go-sdk/shared"
)
func main() {
client := reducto.NewClient(option.WithAPIKey(os.Getenv("REDUCTO_API_KEY")))
// Upload
file, _ := os.Open("invoice.pdf")
defer file.Close()
upload, _ := client.Upload(context.Background(), reducto.UploadParams{
File: reducto.F[io.Reader](file),
})
// Define schema
schema := map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"invoice_number": map[string]interface{}{
"type": "string",
"description": "The invoice number, typically at the top",
},
"total": map[string]interface{}{
"type": "number",
"description": "The total amount due",
},
},
}
// Extract
result, err := client.Extract.Run(context.Background(), reducto.ExtractRunParams{
ExtractConfig: reducto.ExtractConfigParam{
DocumentURL: reducto.F[reducto.ExtractConfigDocumentURLUnionParam](
shared.UnionString(upload.FileID),
),
Schema: reducto.F[interface{}](schema),
},
})
if err != nil {
fmt.Printf("Extract error: %v\n", err)
return
}
// Access extracted values
// Result is []interface{} where each element is a map[string]interface{}
if len(result.Result) > 0 {
extracted := result.Result[0].(map[string]interface{})
fmt.Printf("Invoice Number: %v\n", extracted["invoice_number"])
fmt.Printf("Total: %v\n", extracted["total"])
}
// Usage information
fmt.Printf("Pages processed: %d\n", result.Usage.NumPages)
fmt.Printf("Fields extracted: %d\n", result.Usage.NumFields)
}