Javascript Code Example
Here’s a sample JavaScript code for integrating with the FraudGuard.io API using Node.js and associated explanation.
This JavaScript code using Node.js performs the following steps:
- Sets the FraudGuard.io API credentials (username and password).
- Defines the IP address to query.
- Constructs the API endpoint URL.
- Encodes the credentials using Base64 for basic authentication.
- Sets options for the HTTP request, including the Authorization header.
- Makes an HTTPS request to the API endpoint using https.request.
- Handles the response by concatenating data chunks and logging the response content.
- Handles errors that may occur during the request.
const https = require('https');
// FraudGuard.io API credentials
const username = 'username';
const password = 'password';
// IP address to query
const ipAddress = '8.8.8.8';
// API endpoint
const apiUrl = `https://api.fraudguard.io/ip/${ipAddress}`;
// Basic authentication header
const authString = `${username}:${password}`;
const base64AuthString = Buffer.from(authString).toString('base64');
const authHeaderValue = `Basic ${base64AuthString}`;
// Set options for HTTP request
const options = {
method: 'GET',
headers: {
'Authorization': authHeaderValue
}
};
// Make HTTP request
const req = https.request(apiUrl, options, (res) => {
let data = '';
// A chunk of data has been received
res.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received
res.on('end', () => {
console.log(data);
});
});
// Handle errors
req.on('error', (error) => {
console.error(error);
});
// End the request
req.end();
Make sure to replace ‘username’ and ‘password’ with your actual API credentials.