Documentation
CORSPORXY Logo

Documentation → general

Best Practices

1. Always Use HTTPS

// ✅ Good - secure transmission of API keys
fetch('https://corsproxy.io/?url=https://example.com/api', {
  headers: { 'Authorization': 'Bearer secret-key' }
});

// ❌ Avoid - insecure transmission
fetch('http://corsproxy.io/?url=http://api.example.com/data', {
  headers: { 'Authorization': 'Bearer secret-key' }
});

2. Handle Errors Gracefully

fetch('https://corsproxy.io/?url=https://example.com/api')
  .then(response => {
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }
    return response.json();
  })
  .then(data => console.log(data))
  .catch(error => {
    console.error('Proxy request failed:', error);
    // Show user-friendly error message
  });

3. Cache Responses When Appropriate

// For data that doesn't change frequently
const cacheKey = 'api-data-' + Date.now();
const cachedData = localStorage.getItem(cacheKey);

if (cachedData) {
  console.log(JSON.parse(cachedData));
} else {
  fetch('https://corsproxy.io/?url=https://api.example.com/static-data')
    .then(response => response.json())
    .then(data => {
      localStorage.setItem(cacheKey, JSON.stringify(data));
      console.log(data);
    });
}