Skip to main content

POST /api/shorten

Create a new short link. Anonymous users can create random short codes, while authenticated users can use custom slugs.

Authentication

Authorization
string
Optional for basic usage. Required for custom slugs. Use session-based authentication.

Request Body

url
string
required
The original URL to shorten. Must be a valid HTTP or HTTPS URL.
customSlug
string
Custom short code for the URL. Only available to authenticated users.
expiresInDays
number
Number of days until the link expires.

Response

shortCode
string
The generated or custom short code (8 characters if auto-generated).
shortUrl
string
The complete short URL ready to share.

Error Responses

error
string
Error message describing what went wrong.

Examples

curl -X POST https://shrtnr.app/api/shorten \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/very/long/url/path"
  }'

Success Response

200 OK
{
  "shortCode": "Xy9pQz1m",
  "shortUrl": "https://shrtnr.app/Xy9pQz1m"
}

Error Response

400 Bad Request
{
  "error": "Invalid URL"
}
401 Unauthorized
{
  "error": "Sign in to use a custom slug"
}
Auto-generated short codes use nanoid with 8 characters, providing URL-safe random strings.
Custom slugs are case-sensitive. MyLink and mylink are treated as different slugs.

Build docs developers (and LLMs) love