Getting Started with the Link-Me API
A comprehensive guide to integrating Link-Me's powerful URL shortening API into your applications.
Read MoreSuper Admin
Author
A step-by-step tutorial on integrating Link-Me's API into your Laravel application.
In this tutorial, we'll build a simple link shortener feature in a Laravel application using the Link-Me API.
composer require guzzlehttp/guzzle
Add your API key to .env:
LINKME_API_KEY=your_api_key_here
LINKME_API_URL=https://link-me.com/api/v1
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class LinkMeService
{
protected string $apiKey;
protected string $apiUrl;
public function __construct()
{
$this->apiKey = config('services.linkme.key');
$this->apiUrl = config('services.linkme.url');
}
public function shorten(string $url, ?string $customSlug = null): array
{
$response = Http::withToken($this->apiKey)
->post("{$this->apiUrl}/urls", [
'url' => $url,
'custom_slug' => $customSlug,
]);
return $response->json();
}
public function getStats(string $code): array
{
$response = Http::withToken($this->apiKey)
->get("{$this->apiUrl}/urls/{$code}/stats");
return $response->json();
}
}
<?php
namespace App\Http\Controllers;
use App\Services\LinkMeService;
use Illuminate\Http\Request;
class LinkController extends Controller
{
public function __construct(
protected LinkMeService $linkMe
) {}
public function store(Request $request)
{
$validated = $request->validate([
'url' => 'required|url',
'custom_slug' => 'nullable|string|max:50',
]);
$result = $this->linkMe->shorten(
$validated['url'],
$validated['custom_slug'] ?? null
);
return response()->json($result);
}
}
Route::post('/shorten', [LinkController::class, 'store']);
Always handle potential API errors gracefully:
try {
$result = $this->linkMe->shorten($url);
if (!$result['success']) {
return back()->withErrors(['url' => $result['message']]);
}
return redirect()->back()->with('short_url', $result['data']['short_url']);
} catch (\Exception $e) {
Log::error('Link-Me API error: ' . $e->getMessage());
return back()->withErrors(['url' => 'Unable to shorten URL. Please try again.']);
}
From here, you can expand the integration to include:
Check out our full API documentation for more capabilities.
A comprehensive guide to integrating Link-Me's powerful URL shortening API into your applications.
Read MoreJoin thousands of users who trust Link-Me for their URL shortening needs.