How to Let AI Agents Shorten Your Links Automatically

I wanted my AI coding agent to shorten links for me. Not by opening a browser or switching tabs — by just telling it “shorten this” and getting a tracked short link back. So I built an API for gofwd.to and wired it up to Claude Code. Now I type /shorten https://some-long-url.com and I’m done.

Here’s how to set up the same thing for yourself.

The Idea: AI Agent + URL Shortener API

The concept is simple. You give your AI coding agent (Claude Code, Cursor, Windsurf, whatever you use) a skill or command that calls a URL shortener API. The agent runs a single curl request, parses the response, and hands you back a short link. No tab switching, no copy-paste dance.

But this isn’t just an AI agent trick. The same API works for anything that can make an HTTP request:

You need two things: a URL shortener with an API, and about five minutes. gofwd.to has an API with click tracking built in — plans start at $2/year — so that’s what we’ll use.

Get Your API Key

This takes thirty seconds.

  1. Sign up at gofwd.to (or sign in if you already have an account)
  2. Go to API Keys
  3. Create a key, give it a name, and copy it

You’ll see the full key once — it starts with gofwd_. Store it somewhere safe.

Set it as an environment variable so your tools can use it:

BASH
export GOFWDTO_API_KEY=gofwd_your_key_here
Click to expand and view more

Add that line to your ~/.zshrc or ~/.bashrc to persist it across sessions.

One request, one tracked short link:

BASH
curl -s -X POST https://api.gofwd.to/v1/links \
  -H "Authorization: Bearer $GOFWDTO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/some/very/long/path"}'
Click to expand and view more

Response:

JSON
{
  "id": "abc123",
  "url": "https://example.com/some/very/long/path",
  "short_url": "https://gofwd.to/abc123",
  "expires_at": "2027-02-20T12:00:00Z"
}
Click to expand and view more

That’s it. short_url is your tracked link. Every click on it shows up in your gofwd.to dashboard with analytics. Links expire after one year.

Wire It Up to Claude Code

Here’s where it gets good. Claude Code supports custom skills — slash commands that run predefined workflows. You can create a /shorten skill that calls the API for you.

Create a file at .claude/skills/shorten/SKILL.md in your project (or ~/.claude/skills/shorten/SKILL.md for global access):

MARKDOWN
---
name: shorten
description: Shorten a URL using the gofwd.to API
allowed-tools: Bash(curl *)
---

# Shorten URL

Shorten the given URL using the gofwd.to URL shortener API.

**URL to shorten:** $ARGUMENTS

## Pre-check

First, verify that `GOFWDTO_API_KEY` is set by running
`[ -n "$GOFWDTO_API_KEY" ] && echo "set" || echo "not set"`.
If not set, tell the user to set it with
`export GOFWDTO_API_KEY=gofwd_your_key_here`.
Stop and do not proceed until the key is set.

## Instructions

1. Call the gofwd.to API:

   ```bash
   curl -s -X POST https://api.gofwd.to/v1/links \
     -H "Authorization: Bearer $GOFWDTO_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"url": "<THE_URL>"}'
Click to expand and view more
  1. Read the JSON response and extract the short_url field
  2. Return ONLY the shortened URL to the user
PLAINTEXT

Now you type `/shorten https://example.com/long-url` in Claude Code and get back `https://gofwd.to/abc123`. That's the AI agent angle in practice — one command, no context switch, tracked link in your clipboard.

## The Full API

Three endpoints, all authenticated with your API key.

### Create a Link

```bash
curl -s -X POST https://api.gofwd.to/v1/links \
  -H "Authorization: Bearer $GOFWDTO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}'
Click to expand and view more

Pro users can set a custom short code:

BASH
curl -s -X POST https://api.gofwd.to/v1/links \
  -H "Authorization: Bearer $GOFWDTO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "custom_code": "my-link"}'
Click to expand and view more
BASH
curl -s https://api.gofwd.to/v1/links \
  -H "Authorization: Bearer $GOFWDTO_API_KEY"
Click to expand and view more

Returns all your links with creation and expiration dates:

JSON
{
  "links": [
    {
      "id": "abc123",
      "url": "https://example.com",
      "short_url": "https://gofwd.to/abc123",
      "created_at": "2026-02-20T10:30:45Z",
      "expires_at": "2027-02-20T10:30:45Z"
    }
  ]
}
Click to expand and view more
BASH
curl -s -X DELETE https://api.gofwd.to/v1/links/abc123 \
  -H "Authorization: Bearer $GOFWDTO_API_KEY"
Click to expand and view more

Returns {"message": "Link deleted"} on success.

Code Examples

JavaScript (fetch)

JAVASCRIPT
async function shortenLink(url) {
  const res = await fetch("https://api.gofwd.to/v1/links", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${process.env.GOFWDTO_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ url }),
  });
  const data = await res.json();
  if (!res.ok) throw new Error(data.error);
  return data.short_url;
}

// Usage
const shortUrl = await shortenLink("https://example.com/long-path");
console.log(shortUrl); // https://gofwd.to/abc123
Click to expand and view more

Python (requests)

PYTHON
import os
import requests

def shorten_link(url):
    res = requests.post(
        "https://api.gofwd.to/v1/links",
        headers={
            "Authorization": f"Bearer {os.environ['GOFWDTO_API_KEY']}",
            "Content-Type": "application/json",
        },
        json={"url": url},
    )
    res.raise_for_status()
    return res.json()["short_url"]

# Usage
short_url = shorten_link("https://example.com/long-path")
print(short_url)  # https://gofwd.to/abc123
Click to expand and view more

Error Handling

The API returns standard HTTP status codes with a JSON error field:

StatusMeaningWhat to Do
401Invalid or missing API keyCheck your Authorization header and key
402Monthly link limit reached (Starter plan: 25/month)Upgrade to Pro for unlimited links
403Custom codes require ProEither drop the custom_code field or upgrade
400Bad request (missing URL, invalid input)Check your request body

That’s the whole thing. Get an API key, make a POST request, get a tracked short link. Wire it up to your AI agent, your scripts, your bots — whatever needs to create short links programmatically.

Sign up at gofwd.to and grab your API key from Settings → API Keys . The Starter plan is $2/year and gives you 25 links per month with full click analytics.

Start searching

Enter keywords to search articles

↑↓
↵
ESC
⌘K Shortcut