# Introduction

> Learn how to integrate Dub with Go.

import Prerequisites from "/snippets/sdk-prerequisites.mdx";
import LinkArguments from "/snippets/link-arguments.mdx";

<Prerequisites />

## 2. Install and initialize the Dub Go SDK

<Steps titleSize="h3">

<Step title="Install">

To install the [Dub Go SDK](https://dub.co/sdks/go), run the following command:

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

</Step>

<Step title="Initialize">

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

```go
package main

import (
	"log"
	"os"
	"context"
	dub "github.com/dubinc/dub-go"
)

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

</Step>

</Steps>

## 3. Create link

Let's create a short link using the [Dub Go SDK](https://dub.co/sdks/go).

```go 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)
	}
}
```

<Accordion title="Full list of available attributes for the Link model">
  <LinkArguments />
</Accordion>

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.

```go 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](#5-update-link) or [retrieve analytics](#6-retrieve-analytics-for-link) for it later on using the `externalId` instead of the Dub `linkId`.

## 4. Upsert link

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.

```go 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.

## 5. Update link

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_`).

```go 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)
	}
}
```

## 6. Retrieve analytics for link

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

```go 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

<CardGroup cols={2}>
  <Card
    title="Go Example"
    icon="github"
    href="https://github.com/dubinc/examples/tree/main/go"
    color="#333333"
  >
    See the full example on GitHub.
  </Card>
</CardGroup>
