Dub.co API: Dub is link management infrastructure for companies to create marketing campaigns, link sharing features, and referral programs.
The SDK relies on Composer to manage its dependencies.
To install the SDK and add it as a dependency to an existing composer.json
file:
composer require "dub/dub-php"
declare(strict_types=1);
require 'vendor/autoload.php';
use Dub;
use Dub\Models\Operations;
$security = 'DUB_API_KEY';
$sdk = Dub\Dub::builder()->setSecurity($security)->build();
$request = new Operations\CreateLinkRequestBody(
url: 'https://google.com',
tagIds: [
'clux0rgak00011...',
],
externalId: '123456',
);
$response = $sdk->links->create(
request: $request
);
if ($response->linkSchema !== null) {
// handle response
}
declare(strict_types=1);
require 'vendor/autoload.php';
use Dub;
use Dub\Models\Operations;
$security = 'DUB_API_KEY';
$sdk = Dub\Dub::builder()->setSecurity($security)->build();
$request = new Operations\UpsertLinkRequestBody(
url: 'https://google.com',
tagIds: [
'clux0rgak00011...',
],
externalId: '123456',
);
$response = $sdk->links->upsert(
request: $request
);
if ($response->linkSchema !== null) {
// handle response
}
Available methods
- retrieve - Retrieve analytics for a link, a domain, or the authenticated workspace.
- create - Create a customer
- delete - Delete a customer
- get - Retrieve a customer
- list - Retrieve a list of customers
- update - Update a customer
- create - Create a domain
- delete - Delete a domain
- list - Retrieve a list of domains
- update - Update a domain
- create - Create a new embed token
- list - Retrieve a list of events
- createMany - Bulk create links
- deleteMany - Bulk delete links
- updateMany - Bulk update links
- create - Create a new link
- delete - Delete a link
- get - Retrieve a link
- list - Retrieve a list of links
- count - Retrieve links count
- update - Update a link
- upsert - Upsert a link
- get - Retrieve the metatags for a URL
- get - Retrieve a QR code
Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the
returned object will be a Generator
instead of an individual response.
Working with generators is as simple as iterating over the responses in a foreach
loop, and you can see an example below:
declare(strict_types=1);
require 'vendor/autoload.php';
use Dub;
use Dub\Models\Operations;
$security = 'DUB_API_KEY';
$sdk = Dub\Dub::builder()->setSecurity($security)->build();
$request = new Operations\GetLinksRequest(
page: 1,
pageSize: 50,
);
$responses = $sdk->links->list(
request: $request
);
foreach ($responses as $response) {
if ($response->statusCode === 200) {
// handle response
}
}
Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.
By default an API error will raise a Errors\SDKException
exception, which has the following properties:
Property | Type | Description |
---|---|---|
$message |
string | The error message |
$statusCode |
int | The HTTP status code |
$rawResponse |
?\Psr\Http\Message\ResponseInterface | The raw HTTP response |
$body |
string | The response content |
When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the createMany
method throws the following exceptions:
Error Type | Status Code | Content Type |
---|---|---|
Errors\BadRequest | 400 | application/json |
Errors\Unauthorized | 401 | application/json |
Errors\Forbidden | 403 | application/json |
Errors\NotFound | 404 | application/json |
Errors\Conflict | 409 | application/json |
Errors\InviteExpired | 410 | application/json |
Errors\UnprocessableEntity | 422 | application/json |
Errors\RateLimitExceeded | 429 | application/json |
Errors\InternalServerError | 500 | application/json |
Errors\SDKException | 4XX, 5XX | */* |
declare(strict_types=1);
require 'vendor/autoload.php';
use Dub;
use Dub\Models\Operations;
$security = 'DUB_API_KEY';
$sdk = Dub\Dub::builder()->setSecurity($security)->build();
try {
$request = [
new Operations\RequestBody(
url: 'https://google.com',
tagIds: [
'clux0rgak00011...',
],
externalId: '123456',
),
];
$response = $sdk->links->createMany(
request: $request
);
if ($response->linkSchemas !== null) {
// handle response
}
} catch (Errors\BadRequestThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\UnauthorizedThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\ForbiddenThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\NotFoundThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\ConflictThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\InviteExpiredThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\UnprocessableEntityThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\RateLimitExceededThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\InternalServerErrorThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\SDKException $e) {
// handle default exception
throw $e;
}
The default server can also be overridden globally using the setServerUrl(string $serverUrl)
builder method when initializing the SDK client instance. For example:
declare(strict_types=1);
require 'vendor/autoload.php';
use Dub;
use Dub\Models\Operations;
$security = 'DUB_API_KEY';
$sdk = Dub\Dub::builder()
->setServerURL('https://api.dub.co')
->setSecurity($security)->build();
$request = [
new Operations\RequestBody(
url: 'https://google.com',
tagIds: [
'clux0rgak00011...',
],
externalId: '123456',
),
];
$response = $sdk->links->createMany(
request: $request
);
if ($response->linkSchemas !== null) {
// handle response
}
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
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!