Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update go sample code

Kiran K9646113fae39
diff --git a/apps/docs/quickstart/go.mdx b/apps/docs/quickstart/go.mdx
index 4df4ea8b7e..cd510f5175 100644
--- a/apps/docs/quickstart/go.mdx
+++ b/apps/docs/quickstart/go.mdx
@@ -15,11 +15,9 @@ To install the Dub Go SDK, run the following command:
go get github.com/dubinc/dub-go
```
-## 3. Create link
-
-Let's create a short link using the Dub Go SDK.
+Initialize the Dub Go SDK by creating a new instance of the `Dub` struct.
-```go main.go
+```go
package main
import (
@@ -29,14 +27,46 @@ import (
dub "github.com/dubinc/dub-go"
)
+d := dub.New(
+ dub.WithSecurity(os.Getenv("DUB_API_KEY")),
+ dub.WithWorkspaceID(os.Getenv("ws_clugls3...")),
+)
+```
+
+## 3. Create link
+
+Let's create a short link using the Dub Go SDK.
+
+```go main.go
func main() {
- d := dub.New(
- dub.WithSecurity(os.Getenv("DUB_API_KEY")),
- dub.WithWorkspaceID(os.Getenv("DUB_WORKSPACE_ID")),
- )
+ 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>
+
+## 3a. Create link with `externalId`
+
+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()
@@ -56,11 +86,7 @@ Dub Go SDK provides a method to create or update a link so you don't have to wor
```go main.go
func main() {
- d := dub.New(
- dub.WithSecurity(os.Getenv("DUB_API_KEY")),
- dub.WithWorkspaceID(os.Getenv("DUB_WORKSPACE_ID")),
- )
-
+ // Update the link if same URL already exists or create a new link
request := &operations.UpsertLinkRequestBody{
URL: "https://google.com",
}
@@ -80,19 +106,20 @@ func main() {
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() {
- d := dub.New(
- dub.WithSecurity(os.Getenv("DUB_API_KEY")),
- dub.WithWorkspaceID(os.Getenv("DUB_WORKSPACE_ID")),
- )
-
request := &operations.UpdateLinkRequestBody{
URL: "https://google.us",
}
+ // Update a link by its linkId
ctx := context.Background()
- res, err := d.Links.Update(ctx, "LINK_ID", request)
+ res, err := d.Links.Update(ctx, "clv3o9p9q000au1h0mc7r6l63", request)
if err != nil {
log.Fatal(err)
}
@@ -108,12 +135,21 @@ Let's retrieve analytics for a link using the Dub Go SDK.
```go main.go
func main() {
- d := dub.New(
- dub.WithSecurity(os.Getenv("DUB_API_KEY")),
- dub.WithWorkspaceID(os.Getenv("DUB_WORKSPACE_ID")),
- )
-
// 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 := s.Analytics.Retrieve(ctx, request)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if res.OneOf != nil {
+ // handle response
+ }
}
```