I want to restrict people attending my event based on their zipcode

export async function chkIpAddress() {
    try {
        const httpResponse = await fetch('https://extreme-ip-lookup.com/json', {
            method: 'get'
        });

        if (httpResponse.ok) {
            const json = await httpResponse.json();
            const ipaddress = json.query;
            const country = json.country;
            const city = json.city;
            return { "ipaddress": ipaddress, "country": country, "city": city };
        } else {
            throw new Error('Failed to fetch IP information');
        }
    } catch (error) {
        console.error('Error fetching IP information:', error);
        return null; // or handle the error in an appropriate way for your application
    }
}

Expanding the function to get our ZIP-CODES…

export async function chkIpAddress() {
    try {
        const ipResponse = await fetch('https://extreme-ip-lookup.com/json', {
            method: 'get'
        });

        if (ipResponse.ok) {
            const ipJson = await ipResponse.json();
            const ipaddress = ipJson.query;
            const country = ipJson.country;
            const city = ipJson.city;

            // Use the IP address in your application
            console.log('User IP Address:', ipaddress);

            // Fetch additional details including ZIP code using IPinfo API
            const ipinfoResponse = await fetch(`https://ipinfo.io/${ipaddress}/json`);
            if (ipinfoResponse.ok) {
                const ipinfoJson = await ipinfoResponse.json();
                const zipCode = ipinfoJson.postal;
                console.log('User ZIP Code:', zipCode);

                return { "ipaddress": ipaddress, "country": country, "city": city, "zipCode": zipCode };
            } else {
                throw new Error('Failed to fetch ZIP code information');
            }
        } else {
            throw new Error('Failed to fetch IP information');
        }
    } catch (error) {
        console.error('Error fetching IP information:', error);
        return null; // or handle the error in an appropriate way for your application
    }
}

You also could use the Wix-Window-API…

You will have to modify the presented code to get it to work (just an example).

Similar posts from the past…