Dub.co API: Dub is link management infrastructure for companies to create marketing campaigns, link sharing features, and referral programs.
- SDK Installation
- SDK Example Usage
- Available Resources and Operations
- Pagination
- Retries
- Error Handling
- Server Selection
- Custom HTTP Client
- Authentication
To add the SDK as a dependency to your project:
go get github.com/dubinc/dub-go
package main
import (
"context"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Create(ctx, &operations.CreateLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
})
if err != nil {
log.Fatal(err)
}
if res != nil {
// handle response
}
}
package main
import (
"context"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Upsert(ctx, &operations.UpsertLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateUpsertLinkTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
})
if err != nil {
log.Fatal(err)
}
if res != nil {
// handle response
}
}
Available methods
- Retrieve - Retrieve analytics for a link, a domain, or the authenticated workspace.
- List - Get a list of customers
- Create - Create a customer
- Get - Get a customer
- Update - Update a customer
- Delete - Delete a customer
- Create - Create a domain
- List - Retrieve a list of domains
- Update - Update a domain
- Delete - Delete a domain
- List - Retrieve a list of events
- Create - Create a new link
- List - Retrieve a list of links
- Count - Retrieve links count
- Get - Retrieve a link
- Update - Update a link
- Delete - Delete a link
- CreateMany - Bulk create links
- UpdateMany - Bulk update links
- DeleteMany - Bulk delete links
- Upsert - Upsert a link
- Get - Retrieve the metatags for a URL
- Get - Retrieve a QR code
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 sdkerrors.SDKError
. 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 Create
function may return the following errors:
Error Type | Status Code | Content Type |
---|---|---|
sdkerrors.BadRequest | 400 | application/json |
sdkerrors.Unauthorized | 401 | application/json |
sdkerrors.Forbidden | 403 | application/json |
sdkerrors.NotFound | 404 | application/json |
sdkerrors.Conflict | 409 | application/json |
sdkerrors.InviteExpired | 410 | application/json |
sdkerrors.UnprocessableEntity | 422 | application/json |
sdkerrors.RateLimitExceeded | 429 | application/json |
sdkerrors.InternalServerError | 500 | application/json |
sdkerrors.SDKError | 4XX, 5XX | */* |
package main
import (
"context"
"errors"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"github.com/dubinc/dub-go/models/sdkerrors"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Create(ctx, &operations.CreateLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
})
if err != nil {
var e *sdkerrors.BadRequest
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.Unauthorized
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.Forbidden
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.NotFound
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.Conflict
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.InviteExpired
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.UnprocessableEntity
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.RateLimitExceeded
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.InternalServerError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.SDKError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
}
}
The default server can also be overridden globally using the WithServerURL(serverURL string)
option when initializing the SDK client instance. For example:
package main
import (
"context"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithServerURL("https://api.dub.co"),
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Create(ctx, &operations.CreateLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
})
if err != nil {
log.Fatal(err)
}
if res != nil {
// handle response
}
}
The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
The built-in net/http
client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.
import (
"net/http"
"time"
"github.com/myorg/your-go-sdk"
)
var (
httpClient = &http.Client{Timeout: 30 * time.Second}
sdkClient = sdk.New(sdk.WithClient(httpClient))
)
This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.
This SDK supports the following security scheme globally:
Name | Type | Scheme |
---|---|---|
Token |
http | HTTP Bearer |
You can configure it using the WithSecurity
option when initializing the SDK client instance. For example:
package main
import (
"context"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Create(ctx, &operations.CreateLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
})
if err != nil {
log.Fatal(err)
}
if res != nil {
// handle response
}
}
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"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"github.com/dubinc/dub-go/retry"
"log"
"models/operations"
)
func main() {
s := dubgo.New(
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Create(ctx, &operations.CreateLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
}, 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 != 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"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"github.com/dubinc/dub-go/retry"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithRetryConfig(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}),
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.Create(ctx, &operations.CreateLinkRequestBody{
URL: "https://google.com",
ExternalID: dubgo.String("123456"),
TagIds: dubgo.Pointer(operations.CreateTagIdsArrayOfStr(
[]string{
"clux0rgak00011...",
},
)),
})
if err != nil {
log.Fatal(err)
}
if res != nil {
// handle response
}
}
Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the
returned response object will have a Next
method that can be called to pull down the next group of results. If the
return value of Next
is nil
, then there are no more pages to be fetched.
Here's an example of one such pagination call:
package main
import (
"context"
dubgo "github.com/dubinc/dub-go"
"github.com/dubinc/dub-go/models/operations"
"log"
)
func main() {
s := dubgo.New(
dubgo.WithSecurity("DUB_API_KEY"),
)
ctx := context.Background()
res, err := s.Links.List(ctx, operations.GetLinksRequest{
Page: dubgo.Float64(1),
PageSize: dubgo.Float64(50),
})
if err != nil {
log.Fatal(err)
}
if res != nil {
for {
// handle items
res, err = res.Next()
if err != nil {
// handle error
}
if res == nil {
break
}
}
}
}
While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!