Skip to main content

Examples

Ready-to-use code examples for integrating the IP Geolocation API in various programming languages and frameworks.

Basic Usage

Get Location for Specific IP

// Get location for a specific IP
async function getLocationData(ip) {
  try {
    const response = await fetch(`https://api.sinaty.business/ip/${ip}`);
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.error);
    }
    
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching location data:', error);
    throw error;
  }
}

// Usage example
getLocationData('8.8.8.8')
  .then(data => {
    console.log('Country:', data.country.name);
    console.log('City:', data.city?.name);
    console.log('Coordinates:', data.location);
  })
  .catch(error => {
    console.error('Failed to get location:', error);
  });

Get Current User’s Location

// Get location for the current user's IP
async function getCurrentLocation() {
  try {
    const response = await fetch('https://api.sinaty.business/ip/');
    const data = await response.json();
    
    return {
      country: data.country.name,
      city: data.city?.name || 'Unknown',
      coordinates: data.location,
      timezone: data.location?.time_zone
    };
  } catch (error) {
    console.error('Error getting current location:', error);
    return null;
  }
}

// Usage
getCurrentLocation().then(location => {
  if (location) {
    console.log(`You are in ${location.city}, ${location.country}`);
  }
});

Advanced Usage

POST Request Example

// Use POST method to get location data
async function getLocationViaPost(ip) {
  try {
    const response = await fetch('https://api.sinaty.business/ip/', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ ip: ip })
    });
    
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

Framework Integration

React Hook Example

import { useState, useEffect } from 'react';

function useLocationData(ip = null) {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    async function fetchData() {
      try {
        setLoading(true);
        const url = ip 
          ? `https://api.sinaty.business/ip/${ip}`
          : 'https://api.sinaty.business/ip/';
        
        const response = await fetch(url);
        const result = await response.json();
        
        if (!response.ok) {
          throw new Error(result.error);
        }
        
        setData(result);
      } catch (err) {
        setError(err.message);
      } finally {
        setLoading(false);
      }
    }

    fetchData();
  }, [ip]);

  return { data, loading, error };
}

// Usage in React component
function LocationDisplay({ ip }) {
  const { data, loading, error } = useLocationData(ip);

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;
  if (!data) return <div>No data available</div>;

  return (
    <div>
      <h3>Location Information</h3>
      <p>IP: {data.ip}</p>
      <p>Country: {data.country.name}</p>
      <p>City: {data.city?.name || 'Unknown'}</p>
      {data.location && (
        <p>Coordinates: {data.location.latitude}, {data.location.longitude}</p>
      )}
    </div>
  );
}

Error Handling

Comprehensive Error Handling

async function getLocationWithErrorHandling(ip) {
  try {
    const response = await fetch(`https://api.sinaty.business/ip/${ip}`);
    
    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.error || `HTTP ${response.status}`);
    }
    
    const data = await response.json();
    return { success: true, data };
  } catch (error) {
    return { 
      success: false, 
      error: error.message,
      timestamp: new Date().toISOString()
    };
  }
}

// Usage with error handling
getLocationWithErrorHandling('invalid-ip')
  .then(result => {
    if (result.success) {
      console.log('Location data:', result.data);
    } else {
      console.error('Error:', result.error);
    }
  });

Support

Need help with integration? We’re here to assist:
  • Documentation: Check our detailed API documentation
  • Examples: Review code examples in our guides
  • Email: Contact support@sinaty.business for technical assistance
  • Discord: Join our community for developer discussions
I