PHP code example of rerout / sdk

1. Go to this page and download the library: Download rerout/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rerout / sdk example snippets


use Rerout\Rerout;
use Rerout\Models\CreateLinkInput;

$rerout = new Rerout(getenv('REROUT_API_KEY'));

$link = $rerout->links()->create(new CreateLinkInput(
    targetUrl: 'https://example.com/q4-sale',
    domainHostname: 'go.brand.com',
    code: 'q4',
));

echo $link->shortUrl; // https://go.brand.com/q4

$stats = $rerout->project()->stats(7);
echo "Last 7 days: {$stats->totalClicks} clicks, {$stats->qrScans} QR scans";

$rerout = new Rerout('rrk_…', [
    'base_url' => 'https://api.rerout.co', // optional, default shown
    'timeout' => 30,                       // optional, seconds
    'client' => $guzzleClient,             // optional — inject your own ClientInterface
    'default_headers' => [                 // optional — added to every request
        'User-Agent' => 'my-app/1.0',
    ],
]);

use Rerout\Models\CreateLinkInput;
use Rerout\Models\UpdateLinkInput;

$rerout->links()->create(new CreateLinkInput(
    targetUrl: 'https://example.com',
    domainHostname: 'go.brand.com', // optional
    code: 'promo',                  // optional
    expiresAt: 1893456000,          // optional, unix seconds
    seoTitle: 'Big Sale',           // optional SEO overrides
));

$result = $rerout->links()->list(cursor: null, limit: 50);
foreach ($result->links as $link) {
    echo $link->shortUrl, PHP_EOL;
}
// $result->nextCursor — pass back as `cursor` for the next page

$link = $rerout->links()->get('promo');

// Read-only tags attached to the link. Empty array when none are bound.
foreach ($link->tags as $tag) {
    echo $tag->id, ' ', $tag->name, ' ', $tag->color, PHP_EOL;
}

$link = $rerout->links()->update('promo', new UpdateLinkInput(
    isActive: false,
));

$deleted = $rerout->links()->delete('promo'); // bool

$stats = $rerout->links()->stats('promo', days: 30);

new UpdateLinkInput(
    targetUrl: 'https://example.com/v2',     // set
    expiresAt: UpdateLinkInput::CLEAR,        // null it on the server
    // seoTitle omitted — left untouched
);

$stats = $rerout->project()->stats(days: 30);
echo $stats->totalClicks, ' ', $stats->qrScans;

$me = $rerout->project()->me(); // array{id: string, name: string, slug: string}

use Rerout\Models\QrOptions;

// Pure URL builder — no network call.
$url = $rerout->qr()->url('promo', new QrOptions(
    size: 12,
    margin: 2,
    ecc: 'H',
    domain: 'go.brand.com',
    refresh: true, // true → `refresh=1`; any string is forwarded verbatim
));

// Fetch the rendered SVG (sends the bearer token).
$svg = $rerout->qr()->svg('promo', new QrOptions(size: 8));

use Rerout\Models\CreateWebhookInput;

// Create an endpoint.
$created = $rerout->webhooks()->create(new CreateWebhookInput(
    name: 'Order events',
    url: 'https://example.com/hooks/rerout',
    events: ['link.created', 'link.clicked'],
    isActive: true,        // optional, default true
    payloadFormat: 'json', // optional, 'json' | 'slack'
));
echo $created->endpoint->id;       // wh_…
echo $created->signingSecret;      // whsec_… — shown once, store it now

// List endpoints + every event type the server can deliver.
$result = $rerout->webhooks()->list();
foreach ($result->endpoints as $endpoint) {
    echo $endpoint->url, PHP_EOL;
}
$allEventTypes = $result->eventTypes; // list<string>

// Delete an endpoint (idempotent).
$deleted = $rerout->webhooks()->delete('wh_abc123'); // bool

use Rerout\Webhooks\SignatureVerifier;

$ok = SignatureVerifier::verify(
    rawBody: file_get_contents('php://input'),
    signatureHeader: $_SERVER['HTTP_X_REROUT_SIGNATURE'] ?? '',
    secret: getenv('REROUT_WEBHOOK_SECRET'),
);

if (!$ok) {
    http_response_code(401);
    exit;
}

use Rerout\Models\CreateTagInput;
use Rerout\Models\UpdateTagInput;

// List tags with their link counts.
$result = $rerout->tags()->list();
foreach ($result->tags as $tag) {
    echo "{$tag->name} ({$tag->linkCount} links)", PHP_EOL;
}

// Create a tag. `color` is optional — the server defaults it to 'teal'.
$tag = $rerout->tags()->create(new CreateTagInput(
    name: 'Spring 2026',
    color: 'teal', // optional
));
echo $tag->id; // tag_…

// Update a tag. Only the fields you set are sent; an empty input throws.
$tag = $rerout->tags()->update('tag_abc123', new UpdateTagInput(color: 'red'));

// Delete a tag (also drops its link assignments).
$deleted = $rerout->tags()->delete('tag_abc123'); // bool

use Rerout\Exceptions\ReroutException;

try {
    $rerout->links()->create(new CreateLinkInput(targetUrl: 'http://insecure'));
} catch (ReroutException $e) {
    echo $e->code();        // 'bad_target_url'
    echo $e->status();      // 400
    echo $e->getMessage();  // 'target_url must use https.'
    echo $e->path ?? '';    // the API path that failed
    if ($e->isRateLimited()) { /* back off */ }
    if ($e->isServerError()) { /* retry later */ }
}