Categorygithub.com/documenso/sdk-go
modulepackage
0.0.4
Repository: https://github.com/documenso/sdk-go.git
Documentation: pkg.go.dev

# README

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 here:

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

Documents

Documents.Fields

Documents.Recipients

  • Get - Get document recipient
  • Create - Create document recipient
  • CreateMany - Create document recipients
  • Update - Update document recipient
  • UpdateMany - Update document recipients
  • Delete - Delete document recipient

Templates

Templates.DirectLink

Templates.Fields

Templates.Recipients

  • 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{}, 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{})
	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 TypeStatus CodeContent Type
apierrors.DocumentFindDocumentsResponseBody400application/json
apierrors.DocumentFindDocumentsDocumentsResponseBody404application/json
apierrors.DocumentFindDocumentsDocumentsResponseResponseBody500application/json
apierrors.APIError4XX, 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{})
	if err != nil {

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

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

		var e *apierrors.DocumentFindDocumentsDocumentsResponseResponseBody
		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

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Bool provides a helper function to return a pointer to a bool.
Float32 provides a helper function to return a pointer to a float32.
Float64 provides a helper function to return a pointer to a float64.
Int provides a helper function to return a pointer to an int.
Int64 provides a helper function to return a pointer to an int64.
New creates a new instance of the SDK with the provided options.
Pointer provides a helper function to return a pointer to a type.
String provides a helper function to return a pointer to a string.
WithClient allows the overriding of the default HTTP client used by the SDK.
No description provided by the author
WithSecurity configures the SDK to use the provided security details.
WithSecuritySource configures the SDK to invoke the Security Source function on each method call to determine authentication.
WithServerIndex allows the overriding of the default server by index.
WithServerURL allows the overriding of the default server URL.
WithTemplatedServerURL allows the overriding of the default server URL with a templated URL populated with the provided parameters.
WithTimeout Optional request timeout applied to each operation.

# Variables

ServerList contains the list of servers available to the SDK.

# Structs

No description provided by the author
Documenso v2 beta API: Subject to breaking changes until v2 is fully released.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

HTTPClient provides an interface for suplying the SDK with a custom HTTP client.

# Type aliases

No description provided by the author