Back

Step by Step Guide to Building a SignRequest API Integration in Go

Aug 15, 20245 minute read

Introduction

Hey there, fellow Go developer! Ready to add some digital signature magic to your project? Let's dive into integrating the SignRequest API into your Go application. This powerful tool will let you automate document signing processes, making life easier for you and your users.

Prerequisites

Before we jump in, make sure you've got:

  • Go installed (I know, obvious, right?)
  • A SignRequest API key (grab one from their website)
  • Your favorite code editor

Setting up the project

Let's get this show on the road:

mkdir signrequest-integration cd signrequest-integration go mod init signrequest-integration

Now, let's grab the HTTP client we'll need:

go get github.com/go-resty/resty/v2

Authenticating with the SignRequest API

First things first, let's set up our client:

package main import ( "github.com/go-resty/resty/v2" ) const apiKey = "your-api-key-here" const baseURL = "https://signrequest.com/api/v1/" func main() { client := resty.New() client.SetHeader("Authorization", "Token "+apiKey) client.SetHostURL(baseURL) // We're ready to rock! }

Implementing core functionality

Creating a signature request

Let's create a function to send a signature request:

func createSignatureRequest(client *resty.Client, documentURL string) (string, error) { resp, err := client.R(). SetBody(map[string]interface{}{ "document": documentURL, "signers": []map[string]string{ {"email": "[email protected]"}, }, }). SetResult(map[string]interface{}{}). Post("signrequest-quick-create/") if err != nil { return "", err } result := resp.Result().(*map[string]interface{}) return (*result)["uuid"].(string), nil }

Uploading documents

Before we can request signatures, we need to upload a document:

func uploadDocument(client *resty.Client, filePath string) (string, error) { resp, err := client.R(). SetFile("file", filePath). SetResult(map[string]interface{}{}). Post("documents/") if err != nil { return "", err } result := resp.Result().(*map[string]interface{}) return (*result)["url"].(string), nil }

Handling webhooks

SignRequest can notify your app when things happen. Here's a basic webhook handler:

func webhookHandler(w http.ResponseWriter, r *http.Request) { var event map[string]interface{} json.NewDecoder(r.Body).Decode(&event) // Do something with the event fmt.Printf("Received event: %v\n", event) w.WriteHeader(http.StatusOK) }

Error handling and best practices

Always check for errors and handle them gracefully. Also, respect rate limits:

if resp.StatusCode() == 429 { // Back off and retry time.Sleep(5 * time.Second) // Retry the request }

Testing the integration

Don't forget to test! Here's a simple example:

func TestCreateSignatureRequest(t *testing.T) { client := resty.New() client.SetHeader("Authorization", "Token "+apiKey) client.SetHostURL(baseURL) uuid, err := createSignatureRequest(client, "https://example.com/document.pdf") if err != nil { t.Fatalf("Failed to create signature request: %v", err) } if uuid == "" { t.Fatal("Expected non-empty UUID") } }

Deployment considerations

When deploying, use environment variables for sensitive info:

apiKey := os.Getenv("SIGNREQUEST_API_KEY") if apiKey == "" { log.Fatal("SIGNREQUEST_API_KEY environment variable not set") }

Conclusion

And there you have it! You've just built a SignRequest API integration in Go. Pretty cool, right? Remember, this is just the tip of the iceberg. The SignRequest API has tons more features to explore.

Keep coding, keep learning, and most importantly, keep having fun with Go!