Monitoring IP Lookup API Balance and Setting Alerts

ipapi ~ ipapi.co
2 min readSep 18, 2023

Our subscription plans (for IP address geolocation service) already offer ways to track the usage balance, ensuring you’re never caught off-guard by unexpected shortages. On top of that, we’ve been offering email alerts whenever the lookup balance falls below a certain threshold.

However, as our community grows and diversifies, so do the needs and preferences of our users. Many of our customers have reached out with custom requirements for monitoring and receiving alerts. They’ve expressed the need for more varied and flexible alerting methods beyond just email.

Recognizing the significance of this feedback we’ve crafted this guide. Here, we’ll delve deeper into expanding our notification capabilities to include not only email but also AWS SNS and Slack notifications.

1. Develop a Monitoring Script:

With Python’s requests library, fetching HTTPS requests is straightforward.

import requests

def get_balance():
response = requests.get('https://ipapi.co/quota/')
data = response.json()
return data['available']

2. Setting Up Alerts:

a. Email Alerts:

from smtplib import SMTP
from email.message import EmailMessage

def send_email_alert(balance):
msg = EmailMessage()
msg.set_content(f'Alert! Balance is low: ${balance}')
msg['Subject'] = 'Balance Alert'
msg['From'] = 'alert@yourcompany.com'
msg['To'] = 'your_email@yourcompany.com'

with SMTP('smtp.yourcompany.com') as smtp:
smtp.send_message(msg)

b. AWS SNS Notifications: First, set up an SNS Topic in AWS. Then use boto3, the AWS SDK for Python, to send notifications.

import boto3

def send_sns_alert(balance):
sns_client = boto3.client('sns')
message = f'Alert! Balance is low: ${balance}'
response = sns_client.publish(
TopicArn='YOUR_SNS_TOPIC_ARN',
Message=message,
Subject='Balance Alert'
)

c. Slack Notifications: Utilize incoming webhooks in Slack for notifications. First, create a webhook for your channel.

import requests

def send_slack_alert(balance):
webhook_url = 'YOUR_SLACK_WEBHOOK_URL'
message = {
'text': f'Alert! Balance is low: ${balance}'
}
response = requests.post(webhook_url, json=message)

3. Automate the Process:

# To edit the crontab
crontab -e

# Schedule your script (e.g., hourly)
0 * * * * /usr/bin/python3 /path_to_script/monitor_script.py

4. Using External Micro-service with AWS Lambda:

  1. Zip your script and necessary libraries.
  2. Upload to AWS Lambda.
  3. Set up an Amazon CloudWatch Event for periodic execution.
  4. Employ Amazon SNS or other triggers for alerting.

By actively monitoring the lookup balance and setting up multi-channel notifications, you ensure continuous, undisrupted service delivery. Depending on the urgency and your team’s communication platform, select the alert method that ensures immediate action.

--

--

ipapi ~ ipapi.co

IP Lookup | IP Geolocation API | IP Address Locator by Kloudend, Inc. USA. Trusted by Fortune 500 !