Integrating MiniFyn API in Your Applications
Learn how to integrate MiniFyn's powerful URL shortening API into your applications. This guide covers everything from basic setup to advanced usage patterns, complete with code examples and best practices.
Getting Started with MiniFyn API
Authentication
All API requests require an API key. Pro users can generate keys from their dashboard.
const MINIFYN_API_KEY = 'your_api_key_here';
const BASE_URL = 'https://www.minifyn.com/api';
Basic URL Shortening
Here's a simple example using fetch:
async function shortenUrl(longUrl) {
try {
const response = await fetch(`${BASE_URL}/shorten`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MINIFYN_API_KEY}`
},
body: JSON.stringify({ url: longUrl })
});
const data = await response.json();
return data.shortUrl;
} catch (error) {
console.error('Error shortening URL:', error);
throw error;
}
}
SDK Examples
Node.js Implementation
Create a reusable MiniFyn client:
class MiniFynClient {
constructor(apiKey, options = {}) {
this.apiKey = apiKey;
this.baseUrl = options.baseUrl || 'https://www.minifyn.com/api';
this.timeout = options.timeout || 5000;
}
async shortenUrl(url, options = {}) {
const response = await fetch(`${this.baseUrl}/shorten`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
url,
alias: options.alias,
expiresAt: options.expiresAt
})
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return response.json();
}
async getAnalytics(shortCode) {
const response = await fetch(`${this.baseUrl}/analytics/${shortCode}`, {
headers: {
'Authorization': `Bearer ${this.apiKey}`
}
});
return response.json();
}
}
Python Implementation
import requests
from typing import Optional, Dict
from datetime import datetime
class MiniFynClient:
def __init__(self, api_key: str, base_url: str = "https://www.minifyn.com/api"):
self.api_key = api_key
self.base_url = base_url
self.session = requests.Session()
self.session.headers.update({
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
})
def shorten_url(self, url: str, alias: Optional[str] = None) -> Dict:
response = self.session.post(
f"{self.base_url}/shorten",
json={"url": url, "alias": alias}
)
response.raise_for_status()
return response.json()
def get_analytics(self, short_code: str) -> Dict:
response = self.session.get(
f"{self.base_url}/analytics/{short_code}"
)
response.raise_for_status()
return response.json()
Advanced Usage Patterns
Batch Processing
For handling multiple URLs efficiently:
async function batchShorten(urls) {
const chunks = urls.reduce((acc, url, i) => {
const chunkIndex = Math.floor(i / 10);
if (!acc[chunkIndex]) acc[chunkIndex] = [];
acc[chunkIndex].push(url);
return acc;
}, []);
const results = [];
for (const chunk of chunks) {
const promises = chunk.map(url => shortenUrl(url));
const chunkResults = await Promise.all(promises);
results.push(...chunkResults);
}
return results;
}
Error Handling and Retries
Implement robust error handling:
class MiniFynError extends Error {
constructor(message, status, code) {
super(message);
this.status = status;
this.code = code;
}
}
async function reliableShortenUrl(url, maxRetries = 3) {
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await shortenUrl(url);
} catch (error) {
lastError = error;
// Don't retry on client errors
if (error.status >= 400 && error.status < 500) {
throw error;
}
// Wait before retrying
await new Promise(resolve =>
setTimeout(resolve, Math.pow(2, attempt) * 1000)
);
}
}
throw lastError;
}
Rate Limiting Handler
Implement rate limit handling:
class RateLimiter {
constructor(maxRequests, timeWindow) {
this.maxRequests = maxRequests;
this.timeWindow = timeWindow;
this.requests = [];
}
async acquireToken() {
const now = Date.now();
this.requests = this.requests.filter(
time => time > now - this.timeWindow
);
if (this.requests.length >= this.maxRequests) {
const oldestRequest = this.requests[0];
const waitTime = oldestRequest + this.timeWindow - now;
await new Promise(resolve => setTimeout(resolve, waitTime));
}
this.requests.push(now);
}
}
Analytics Integration
Tracking and Reporting
Implement analytics tracking:
class AnalyticsTracker {
constructor(minifynClient) {
this.client = minifynClient;
this.cache = new Map();
}
async trackClick(shortCode) {
const analytics = await this.client.getAnalytics(shortCode);
this.cache.set(shortCode, analytics);
return analytics;
}
async generateReport(shortCodes) {
const results = await Promise.all(
shortCodes.map(code => this.trackClick(code))
);
return {
totalClicks: results.reduce((sum, r) => sum + r.clicks, 0),
byCountry: this.aggregateByCountry(results),
byDevice: this.aggregateByDevice(results)
};
}
}
Security Best Practices
API Key Management
Secure API key handling:
class SecureKeyManager {
constructor() {
this.keys = new Map();
}
addKey(name, key) {
// Encrypt key before storing
this.keys.set(name, this.encrypt(key));
}
getKey(name) {
const encryptedKey = this.keys.get(name);
return encryptedKey ? this.decrypt(encryptedKey) : null;
}
rotateKey(name, newKey) {
const oldKey = this.getKey(name);
this.addKey(name, newKey);
return oldKey;
}
}
Testing and Monitoring
Integration Tests
Example test suite:
describe('MiniFyn API Integration', () => {
const client = new MiniFynClient(process.env.TEST_API_KEY);
test('should shorten URL successfully', async () => {
const longUrl = 'https://example.com/very/long/url';
const result = await client.shortenUrl(longUrl);
expect(result.shortUrl).toBeDefined();
expect(result.originalUrl).toBe(longUrl);
});
test('should handle rate limiting', async () => {
const urls = Array(20).fill('https://example.com');
const results = await batchShorten(urls);
expect(results).toHaveLength(urls.length);
});
});
Conclusion
The MiniFyn API provides a robust platform for URL shortening integration. Key takeaways:
Always handle rate limits appropriately
Implement proper error handling and retries
Secure your API keys
Monitor API usage and performance
Test your integration thoroughly
For more information and support:
Visit our API documentation
Join our developer community
Follow our blog for updates
Contact support for assistance
Remember to keep your integration up to date with the latest API features and best practices.