Advanced8 min6 stepsBusiness

Make your first API request

Create an API key, exchange it for an access token, then shorten a URL and list your links as JSON.

Open API Keys

The Lnki API creates and manages the same links, QR codes and imports you see in the dashboard. In a few minutes you will have a key, a token and a short link made by your own code.

Before you start

  • A workspace on the Business plan, which includes API access at 5,000 requests per minute for each key.
  • A terminal with curl, or any HTTP client.
  1. Create an API key

    Open API Keys and choose Create New API Key. Name it after the app that will use it, for example Production app, and choose Create Key.

    The Create API Key dialog with the key name Production app
  2. Copy the key now

    The full key, starting with lnki_live_, is shown once. Copy it into your secret manager or an environment variable. Afterwards the page lists only its first characters, together with its status, last use and request count.

    The API Keys page showing one active key, its request count and the plan's rate limit

    Treat a key like a password: keep it on your server, never in a web page or a mobile app. If one leaks, choose Regenerate or Revoke from its row menu; every token made from it stops working at once.

  3. Exchange the key for an access token

    The key itself is only sent to the token endpoint. Post it there and you get an access token that lasts one hour, plus a refresh token for getting the next one.

    The response's access_token starts with lnki_at_. Keep it, and the refresh_token, for the following steps.

    Get an access token
    curl -X POST https://lnki.io/api/v1/oauth/token \
      -H "Content-Type: application/json" \
      -d '{
        "grant_type": "api_key",
        "api_key": "YOUR_API_KEY"
      }'
  4. Create a short link

    Send a POST to /api/v1/links with the long URL and the access token as a Bearer token. Only destination_url is required.

    The API answers 201 Created with the new link. short_host_path is the short link without its scheme, here lnki.io/summer-sale. Open the dashboard and the link is in My Links.

    Create a link
    curl -X POST https://lnki.io/api/v1/links \
      -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "destination_url": "https://example.com/summer-sale",
        "slug": "summer-sale"
      }'
  5. List your links

    A GET on the same path returns your links a page at a time. Add q to search and sort to order them. Every response carries X-RateLimit-Remaining, so you can see how much of this minute's allowance is left.

    List links
    curl "https://lnki.io/api/v1/links?per_page=25&sort=clicks-desc" \
      -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
  6. Refresh the token when it expires

    After an hour a request answers 401 with the code token_expired. Post the refresh token to the same token endpoint and you get a new pair. A refresh token works once, so store the new one each time.

    Refresh the token
    curl -X POST https://lnki.io/api/v1/oauth/token \
      -H "Content-Type: application/json" \
      -d '{
        "grant_type": "refresh_token",
        "refresh_token": "YOUR_REFRESH_TOKEN"
      }'

What you have now

Your code can now authenticate, create and read links. The API reference covers every parameter, QR codes, bulk imports, errors and rate limits, and has a console to try each request.

Keep going