Skip to content

documenso/sdk-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Documenso Logo

 

Documenso Go SDK

A SDK for seamless integration with Documenso v2 API.

The full Documenso API can be viewed here (todo), which includes examples.

⚠️ Warning

Documenso v2 API and SDKs are currently in beta. There may be to breaking changes.

To keep updated, please follow the discussions and issues here:

  • Discussion -> Todo
  • Breaking change alerts -> Todo

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/documenso/sdk-go

Authentication

To use the SDK, you will need a Documenso API key which can be created here.

documenso := documensoSdk.New(
  documensoSdk.WithSecurity(os.Getenv("DOCUMENSO_API_KEY")),
)

Document creation example

Currently creating a document involves two steps:

  1. Create the document
  2. Upload the PDF

This is a temporary measure, in the near future prior to the full release we will merge these two tasks into one request.

Here is a full example of the document creation process which you can copy and run.

Note that the function is temporarily called createV0, which will be replaced by create once we resolve the 2 step workaround.

package main

import (
	"bytes"
	"context"
	"errors"
	"io"
	"log"
	"net/http"
	"os"

	documensoSdk "github.com/documenso/sdk-go"
	"github.com/documenso/sdk-go/models/operations"
)

func uploadFileToPresignedUrl(filePath string, uploadUrl string) error {
	// Read file
	fileContent, err := os.ReadFile(filePath)
	if err != nil {
		return err
	}

	// Create request
	req, err := http.NewRequest("PUT", uploadUrl, bytes.NewReader(fileContent))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/octet-stream")

	// Make request
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Check response
	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(resp.Body)
		return errors.New("upload failed with status: " + resp.Status + " body: " + string(body))
	}

	return nil
}

func main() {
	ctx := context.Background()

	documenso := documensoSdk.New(
		documensoSdk.WithSecurity("<API_KEY>"),
	)

	res, err := documenso.Documents.CreateV0(ctx, operations.DocumentCreateDocumentTemporaryRequestBody{
		Title: "Document title",
		Meta: &operations.Meta{
			Subject:              documensoSdk.String("Email subject"),
			Message:              documensoSdk.String("Email message"),
			TypedSignatureEnabled: documensoSdk.Bool(false),
		},
	})


	if err != nil {
		log.Fatal(err)
	}


	// Upload file
	err = uploadFileToPresignedUrl("./demo.pdf", res.Object.UploadURL)

	if err != nil {
		log.Fatal(err)
	}
}

Available Resources and Operations

Available methods
  • Get - Get document recipient
  • Create - Create document recipient
  • CreateMany - Create document recipients
  • Update - Update document recipient
  • UpdateMany - Update document recipients
  • Delete - Delete document recipient
  • Get - Get template recipient
  • Create - Create template recipient
  • CreateMany - Create template recipients
  • Update - Update template recipient
  • UpdateMany - Update template recipients
  • Delete - Delete template recipient

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	sdkgo "github.com/documenso/sdk-go"
	"github.com/documenso/sdk-go/models/operations"
	"github.com/documenso/sdk-go/retry"
	"log"
	"models/operations"
	"os"
)

func main() {
	ctx := context.Background()

	s := sdkgo.New(
		sdkgo.WithSecurity(os.Getenv("DOCUMENSO_API_KEY")),
	)

	res, err := s.Documents.Find(ctx, operations.DocumentFindDocumentsRequest{
		OrderByDirection: operations.OrderByDirectionDesc.ToPointer(),
	}, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	sdkgo "github.com/documenso/sdk-go"
	"github.com/documenso/sdk-go/models/operations"
	"github.com/documenso/sdk-go/retry"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := sdkgo.New(
		sdkgo.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		sdkgo.WithSecurity(os.Getenv("DOCUMENSO_API_KEY")),
	)

	res, err := s.Documents.Find(ctx, operations.DocumentFindDocumentsRequest{
		OrderByDirection: operations.OrderByDirectionDesc.ToPointer(),
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.

By Default, an API error will return apierrors.APIError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the Find function may return the following errors:

Error Type Status Code Content Type
apierrors.ErrorBADREQUEST 400 application/json
apierrors.ErrorNOTFOUND 404 application/json
apierrors.ERRORINTERNALSERVERERROR 500 application/json
apierrors.APIError 4XX, 5XX */*

Example

package main

import (
	"context"
	"errors"
	sdkgo "github.com/documenso/sdk-go"
	"github.com/documenso/sdk-go/models/apierrors"
	"github.com/documenso/sdk-go/models/operations"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := sdkgo.New(
		sdkgo.WithSecurity(os.Getenv("DOCUMENSO_API_KEY")),
	)

	res, err := s.Documents.Find(ctx, operations.DocumentFindDocumentsRequest{
		OrderByDirection: operations.OrderByDirectionDesc.ToPointer(),
	})
	if err != nil {

		var e *apierrors.ErrorBADREQUEST
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *apierrors.ErrorNOTFOUND
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *apierrors.ERRORINTERNALSERVERERROR
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *apierrors.APIError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

SDK Created by Speakeasy