POST /api/v1/links
200 { "shortUrl": "xpol.app/a3k" }

URL Shortener API Guide: Programmatically Create and Manage Short Links

·7 min read

Creating short links through a dashboard works for one-off tasks, but when you need to generate hundreds of links from a spreadsheet, integrate link creation into a deployment pipeline, or build short URLs into a customer-facing product, you need an API. This guide walks through the fundamentals of working with a URL shortener API, with practical code examples you can adapt to your workflow.

Why Use an API for Link Shortening?

  • Automation— generate links automatically when a blog post is published, a product is launched, or an email campaign is scheduled.
  • CI/CD integration— create preview links for pull request deployments, QA environments, or release notes.
  • Bulk operations— import thousands of URLs from a CSV and create branded short links for each one in a single script.
  • Custom integrations— embed link creation into internal tools, CRMs, or marketing platforms that don't have native integrations.
  • Programmatic analytics— pull click data into your own dashboards, data warehouses, or reporting tools.

Getting Started: Create an API Key

Before making any API calls, you need an API key. In Xpolink, navigate to Dashboard → Settings → API Keys and click Create Key. Give it a descriptive name (e.g., marketing-automation) so you can identify it later.

The key is shown once. Copy it immediately and store it securely — in a password manager or an environment variable, never in source code. You can revoke and regenerate keys at any time.

Authentication

All API requests are authenticated with a Bearer token in the Authorization header:

Authorization: Bearer xpo_live_k7mP2xR9...

Requests without a valid key return a 401 Unauthorized response. Keys are scoped to your workspace, so all team members' links and analytics are accessible.

Code Examples

Create a Short Link (curl)

curl -X POST https://xpolink.app/api/v1/links \
  -H "Authorization: Bearer $XPOLINK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/blog/my-new-post",
    "shortCode": "new-post",
    "domain": "go.yourcompany.com",
    "title": "Blog: My New Post"
  }'

The response includes the full short URL, the link ID (for future updates), and a QR code download URL. The slug and domainfields are optional — omit them to use a random slug on your default domain.

List Links

curl https://xpolink.app/api/v1/links?page=1 \
  -H "Authorization: Bearer $XPOLINK_API_KEY"

Returns a paginated list of links with their metadata and click counts. Use the search query parameter to filter by short code, URL, or title.

Get Link Analytics

curl https://xpolink.app/api/v1/links/abc123/stats \
  -H "Authorization: Bearer $XPOLINK_API_KEY"

Returns click counts broken down by day, device, country, referrer, and browser. Use this endpoint to feed data into your own analytics pipeline or reporting tool.

Bulk Import via API

Need to shorten hundreds of URLs at once? The import endpoint accepts up to 500 links per request (Pro and Business plans):

curl -X POST https://xpolink.app/api/v1/links/import \
  -H "Authorization: Bearer $XPOLINK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '[
    { "url": "https://example.com/page-1", "title": "Page 1" },
    { "url": "https://example.com/page-2", "title": "Page 2" },
    { "url": "https://example.com/page-3", "title": "Page 3" }
  ]'

The response includes a summary with created, failed, and total counts plus per-row results. For larger imports, loop through your data in batches of 500.

CLI as an Alternative

If you prefer working in the terminal but don't need the full flexibility of raw HTTP requests, the Xpolink CLI wraps the API in a developer-friendly command-line tool:

# Create a link
xpolink shorten https://example.com/page --slug my-page

# List recent links
xpolink links list --limit 10

# Get stats for a link
xpolink links stats lnk_abc123 --format json

The CLI authenticates using the same API key, stored in ~/.xpolink/config.json or the XPOLINK_API_KEY environment variable.

Webhook and Automation Ideas

Once you're comfortable with the API, here are some workflows teams commonly build:

  • CMS publish hook— when a new blog post goes live, a webhook triggers a function that creates a branded short link and posts it to Slack.
  • Email campaign automation— before sending a newsletter, a script shortens all links in the email body so every click is tracked through your link dashboard.
  • QR code generation pipeline— bulk-create links for product SKUs and download QR codes programmatically for packaging designers.
  • Weekly reports— a scheduled job pulls click stats from the API and sends a summary to your team via email or Slack.
  • Expiring preview links— CI creates a short link for each PR preview deployment with a 7-day expiry, so links auto-clean after the PR merges.

Next Steps

The full API reference is available at the Xpolink API docs, including request/response schemas, error codes, rate limits, and pagination details. Create a free account to get an API key and start building.

Ready to create branded short links?

Start free with 50 links per month. No credit card required.

Get Started Free
Back to blog