Getting started overview

This guide outlines the process of initiating integration with the Shrtcode API, focusing on the essential steps required to shorten URLs programmatically. The Shrtcode API is designed for simple URL shortening, offering a direct interface for developers to integrate link generation into their applications without extensive setup. The process typically involves creating an account, obtaining an API key for authentication, and then constructing an HTTP request to the API's shortening endpoint. This page provides a focused approach to achieve a working integration quickly, enabling developers to generate short URLs programmatically. For detailed information beyond the scope of a first integration, refer to the official Shrtcode API documentation.

The Shrtcode API utilizes a RESTful architecture, accepting HTTP POST requests and returning JSON responses. Authentication is managed through an API key, which must be included as a Bearer Token in the Authorization header of each request. This method is a common approach for securing web APIs, ensuring that only authorized applications can interact with the service. Developers can use various programming languages and HTTP client libraries to interact with the API, with official SDKs available to streamline development. Understanding the basics of RESTful API interaction and JSON data structures will facilitate a smoother integration process. For a general overview of REST principles, refer to the Mozilla Developer Network's REST definition.

Quick Reference Table

Step What to Do Where
1. Account Creation Register for a new Shrtcode account. Shrtcode Homepage
2. API Key Retrieval Locate and copy your unique API key from your dashboard. Shrtcode Dashboard (after login)
3. Prepare Request Construct an HTTP POST request with the target URL and API key. Your development environment
4. Send Request Execute the POST request to the Shrtcode API endpoint. Your development environment
5. Process Response Parse the JSON response to extract the shortened URL. Your development environment

Create an account and get keys

To begin using the Shrtcode API, you must first create an account on the official Shrtcode website. Account registration is a prerequisite for obtaining an API key, which is essential for authenticating your requests to the API. The registration process typically involves providing an email address and creating a password.

  1. Navigate to the Shrtcode Website: Open your web browser and go to the Shrtcode homepage.
  2. Locate the Sign-Up Option: Look for a "Sign Up" or "Register" button, usually found in the top-right corner of the page.
  3. Complete the Registration Form: Enter the required information, such as your email address and a secure password. Agree to any terms of service or privacy policies, if prompted.
  4. Verify Your Email (if required): Some services require email verification to activate your account. Check your inbox for a verification email and follow the instructions to confirm your address.
  5. Log In to Your Account: Once registered and verified, log in to your newly created Shrtcode account using your credentials.

API Key Retrieval

After successfully logging into your Shrtcode account, you will need to locate your API key. This key acts as your credential for accessing the Shrtcode API and must be kept confidential. It is typically found within your user dashboard or a dedicated "API Settings" or "Developer" section.

  1. Access Your Dashboard: Upon logging in, you should be directed to your user dashboard. If not, look for a link labeled "Dashboard" or "My Account."
  2. Find API Settings: Within the dashboard, navigate to the section related to API access or developer settings. This might be labeled "API Keys," "Developer Settings," or similar.
  3. Generate or Copy Your API Key: Your API key should be displayed on this page. If it's your first time, you might need to click a button to "Generate New API Key." Once displayed, copy the entire key. This key is a unique string of characters that authenticates your requests.
  4. Secure Your API Key: Treat your API key like a password. Do not hardcode it directly into client-side code, commit it to public repositories, or share it unnecessarily. Best practices suggest storing API keys as environment variables or using a secure secrets management system, especially in production environments, to prevent unauthorized access. For guidance on secure API key handling, review best practices for securing API keys in cloud environments.

Your first request

With an account established and your API key secured, you can now make your first request to the Shrtcode API to shorten a URL. The API expects a POST request to its primary endpoint, with the long URL provided in the request body.

API Endpoint

The base URL for the Shrtcode API is https://api.shrtcode.de/v2/shorten.

Request Format

The API requires a POST request with a JSON body containing the url field, which holds the long URL you wish to shorten. The Content-Type header must be set to application/json. Authentication is handled via the Authorization header using a Bearer Token.

HTTP Headers:

  • Content-Type: application/json
  • Authorization: Bearer YOUR_API_KEY (Replace YOUR_API_KEY with your actual API key)

Request Body (JSON):


{
  "url": "https://www.example.com/a-very-long-url-that-needs-shortening"
}

Example using cURL

cURL is a command-line tool and library for transferring data with URLs. It is commonly used for testing API endpoints. Replace YOUR_API_KEY with the key you retrieved from your Shrtcode dashboard and https://www.example.com/a-very-long-url-that-needs-shortening with the URL you want to shorten.


curl -X POST \
  https://api.shrtcode.de/v2/shorten \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{"url": "https://www.example.com/a-very-long-url-that-needs-shortening"}'

Example using JavaScript (Fetch API)

For web applications, the Fetch API is a standard way to make HTTP requests. This example demonstrates how to shorten a URL using JavaScript.


const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const longUrl = 'https://www.example.com/another-long-url-for-testing';

fetch('https://api.shrtcode.de/v2/shorten', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${apiKey}`
  },
  body: JSON.stringify({ url: longUrl })
})
.then(response => {
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return response.json();
})
.then(data => {
  console.log('Shortened URL:', data.result.shrtlnk);
})
.catch(error => {
  console.error('Error shortening URL:', error);
});

Expected Response

A successful request (HTTP status 200 OK) will return a JSON object containing the original URL, the shortened URL, and other related information.


{
  "ok": true,
  "result": {
    "shrtlnk": "https://shrtco.de/example",
    "full_shrtlnk": "https://shrtco.de/example",
    "original_link": "https://www.example.com/a-very-long-url-that-needs-shortening"
  }
}

The shrtlnk field within the result object contains the shortened URL generated by Shrtcode.

Common next steps

After successfully making your first API call, consider these common next steps to further integrate Shrtcode into your applications:

  • Integrate into Your Application: Incorporate the API call into your application's logic. This might involve creating a function or module that takes a long URL and returns the shortened version, which can then be displayed to users or stored in a database.
  • Error Handling: Implement robust error handling for API responses. The API may return various HTTP status codes (e.g., 400 for bad request, 401 for unauthorized, 429 for rate limit exceeded) and error messages. Your application should be able to gracefully handle these scenarios, perhaps by retrying the request, logging the error, or informing the user. For a comprehensive list of status codes, consult the Shrtcode API documentation.
  • Rate Limiting Management: Be aware of the API's rate limits, especially on the free tier (100 requests/day). For applications requiring higher volumes, consider upgrading your plan or implementing a caching mechanism for frequently shortened URLs to reduce API calls.
  • SDK Utilization: Explore the official SDKs provided by Shrtcode for languages like JavaScript, Python, PHP, Go, Ruby, Java, C#, Swift, and Kotlin. Using an SDK can simplify API interactions by abstracting away the HTTP request details and providing language-specific methods for common operations. Refer to the Shrtcode SDKs documentation for installation and usage instructions.
  • Monitoring and Logging: Set up monitoring and logging for your API calls. This helps track API usage, identify potential issues, and understand the performance of your integration.
  • Explore Advanced Features (if applicable): While Shrtcode focuses on core shortening, if your needs evolve, you might explore alternatives like Bitly or Rebrandly for features such as custom domains, analytics, or QR code generation.

Troubleshooting the first call

Encountering issues during your first API call is common. Here are some troubleshooting steps and common problems to address:

  • Incorrect API Key: Double-check that you have copied your API key correctly and that there are no leading or trailing spaces. An incorrect key will typically result in a 401 Unauthorized error.
  • Missing or Incorrect Authorization Header: Ensure the Authorization header is present and correctly formatted as Bearer YOUR_API_KEY. Any deviation can lead to authentication failures.
  • Incorrect Content-Type Header: The API expects Content-Type: application/json. If this header is missing or incorrect, the API may not be able to parse your request body, resulting in a 400 Bad Request.
  • Invalid JSON Body: Verify that your request body is valid JSON. Syntax errors in the JSON (e.g., missing commas, unquoted keys) will cause a 400 Bad Request error. Use an online JSON validator if unsure.
  • Invalid URL in Request Body: The url field in your JSON body must contain a valid, properly formatted URL. Ensure it includes the scheme (http:// or https://).
  • Rate Limiting Exceeded (429 Too Many Requests): If you make too many requests within a short period, especially on the free tier, you might hit the rate limit. Wait for the rate limit to reset or upgrade your plan. The API response for a 429 error often includes headers indicating when you can retry.
  • Network Issues: Ensure your development environment has an active internet connection and no firewalls are blocking outgoing HTTP requests to https://api.shrtcode.de.
  • Review API Documentation: For specific error codes or unexpected responses, always refer to the official Shrtcode API documentation. It provides the most accurate and up-to-date information on API behavior and error messages.
  • Check Server Status: Occasionally, API services can experience downtime. Check the Shrtcode website or any provided status pages for service announcements if you suspect a broader issue.