Skip to main content

Setup

Go Report Card Go Reference

Welcome to the official Go SDK documentation for Nanonets. This guide will help you integrate Nanonets into your Go applications for intelligent document processing, data extraction, and workflow automation.

The Nanonets Go SDK provides a simple and intuitive interface to interact with the Nanonets API.

Installation

Install the SDK using go get:

go get github.com/NanoNets/nanonets-go/nanonets

Authentication

The SDK uses your API key for authentication. You can set it in two ways:

  1. Environment variable:
export NANONETS_API_KEY='your_api_key'
  1. Direct initialization:
import (
"github.com/NanoNets/nanonets-go/nanonets"
)

client := nanonets.NewClient("your_api_key")

Quick Start

Here's a quick example of how to use the SDK:

package main

import (
"fmt"
"github.com/NanoNets/nanonets-go/nanonets"
)

func main() {
client := nanonets.NewClient("your_api_key")

// Create a workflow
workflow, err := client.Workflows.Create(nanonets.CreateWorkflowRequest{
Description: "SDK Example Workflow",
WorkflowType: "", // Instant learning
})
if err != nil {
fmt.Println("Error creating workflow:", err)
return
}
workflowID := workflow.ID

// Configure fields and table headers
fields := []nanonets.Field{
{Name: "invoice_number"},
{Name: "total_amount"},
{Name: "invoice_date"},
}
tableHeaders := []nanonets.TableHeader{
{Name: "item_description"},
{Name: "quantity"},
{Name: "unit_price"},
{Name: "total"},
}
err = client.Workflows.SetFields(workflowID, nanonets.SetFieldsRequest{
Fields: fields,
TableHeaders: tableHeaders,
})
if err != nil {
fmt.Println("Error setting fields:", err)
return
}

// Upload a document to process
uploadResult, err := client.Documents.Upload(workflowID, nanonets.UploadDocumentRequest{
File: "/path/to/document.pdf",
Async: false,
Metadata: map[string]string{"test": "true"},
})
if err != nil {
fmt.Println("Error uploading document:", err)
return
}
fmt.Println("Upload result:", uploadResult)
}

Features

The SDK provides comprehensive functionality for:

Error Handling

The SDK provides custom error types for better error handling:

import (
"github.com/NanoNets/nanonets-go/nanonets"
)

workflow, err := client.Workflows.Create(...)
if err != nil {
switch err.(type) {
case *nanonets.AuthenticationError:
fmt.Println("Authentication failed:", err)
case *nanonets.ValidationError:
fmt.Println("Invalid input:", err)
case *nanonets.NanonetsError:
fmt.Println("An error occurred:", err)
}
}

Best Practices

  1. Resource Management

    // Use defer for cleanup
    func processDocument(client *nanonets.Client, document string) error {
    // Process document
    defer func() {
    // Cleanup if needed
    }()
    return nil
    }
  2. Batch Processing

    // Process documents in batches
    func processBatch(client *nanonets.Client, documents []string, batchSize int) error {
    for i := 0; i < len(documents); i += batchSize {
    end := i + batchSize
    if end > len(documents) {
    end = len(documents)
    }
    batch := documents[i:end]
    // Process batch
    }
    return nil
    }
  3. Error Recovery

    import "github.com/cenkalti/backoff"

    // Retry with exponential backoff
    func processWithRetry(client *nanonets.Client, document string) error {
    operation := func() error {
    return processDocument(client, document)
    }
    return backoff.Retry(operation, backoff.NewExponentialBackOff())
    }

For more detailed information about specific features, please refer to the following sections:

For information about working with fields and tables, see Fields and Tables.