1. Prerequisites

To follow this guide, you will need to:

2. Install and initialize the Dub Go SDK

1

Install

To install the Dub Go SDK, run the following command:

bash
go get github.com/dubinc/dub-go
2

Initialize

Initialize the Dub Go SDK by creating a new instance of the Dub struct.

package main

import (
	"log"
	"os"

	dub "github.com/dubinc/dub-go"
)

d := dub.New(
	dub.WithSecurity(os.Getenv("DUB_API_KEY")),
)

Let’s create a short link using the Dub Go SDK.

main.go
func main() {
	request := &operations.CreateLinkRequestBody{
		URL: "https://google.com",
	}

	ctx := context.Background()
	res, err := d.Links.Create(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.LinkSchema != nil {
		fmt.Println(res.LinkSchema.ShortLink)
	}
}

Optionally, you can also pass an externalId field which is a unique identifier for the link in your own database to associate it with the link in Dub’s system.

main.go
func main() {
	request := &operations.CreateLinkRequestBody{
		URL: "https://google.com",
		ExternalId: "12345"
	}

	ctx := context.Background()
	res, err := d.Links.Create(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.LinkSchema != nil {
		fmt.Println(res.LinkSchema.ShortLink)
	}
}

This will let you easily update the link or retrieve analytics for it later on using the externalId instead of the Dub linkId.

Dub Go SDK provides a method to upsert a link – where an existing link is updated if it exists, or a new link is created if it doesn’t. so you don’t have to worry about checking if the link already exists.

main.go
func main() {
	// Update the link if same URL already exists or create a new link
	request := &operations.UpsertLinkRequestBody{
		URL: "https://google.com",
	}

	ctx := context.Background()
	res, err := d.Links.Upsert(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.LinkSchema != nil {
		fmt.Println(res.LinkSchema.ShortLink)
	}
}

This way, you won’t have to worry about checking if the link already exists when you’re creating it.

Let’s update an existing link using the Dub Go SDK.

You can do that in two ways:

  • Using the link’s linkId in Dub’s system.
  • Using the link’s externalId in your own database (prefixed with ext_).
main.go
func main() {
	request := &operations.UpdateLinkRequestBody{
		URL: "https://google.us",
	}

	// Update a link by its linkId
	ctx := context.Background()
	res, err := d.Links.Update(ctx, "clv3o9p9q000au1h0mc7r6l63", request)
	if err != nil {
		log.Fatal(err)
	}
	if res.LinkSchema != nil {
		fmt.Println(res.LinkSchema.ShortLink)
	}
}

Let’s retrieve analytics for a link using the Dub Go SDK.

main.go
func main() {
	// Retrieve the timeseries analytics for the last 7 days for a link
	request := operations.RetrieveAnalyticsRequest{
		LinkId: "clv3o9p9q000au1h0mc7r6l63",
		Interval: "7d",
		GroupBy: "timeseries"
	}

	ctx := context.Background()
	res, err := d.Analytics.Retrieve(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.OneOf != nil {
		// handle response
	}
}

7. Examples