Before sending a key check it’s good practice to check for an internet connection first via a ping. This will solve most of your connection errors.
If a license key check fails due to a connection error it’s reasonable to send the check again. This could be done right away or a short time after, but do not send continuous checks until a success message is received. These will almost always fail in quick succession if the original check failed, and then your end user will be timed out by our AI security systems and unable to receive any more license key checks for a time.
Making A Connection #
The proper way to build a license key check is to:
- Ping a large domain such as google.com or a dedicated pinging service
- If the ping fails, assume the end user doesn’t have an internet connection
- If successful, send the key check to our API
- Then, if the key check fails during the check due to a connection error, ping https://batchkeys.com
- If successful, try the key again up to three times
- If the ping failed, wait some times and try pinging https://batchkeys.com again
- Then, if the key check fails during the check due to a connection error, ping https://batchkeys.com
Here is a sample code in Python utilizing these practices.
import os
from dotenv import load_dotenv
import requests
import subprocess
import time
# Load environment variables from .env file
load_dotenv()
def ping(domain):
# Using the 'ping' command to check connectivity
try:
output = subprocess.check_output(['ping', '-c', '1', domain], stderr=subprocess.STDOUT, universal_newlines=True)
return True
except subprocess.CalledProcessError:
return False
def check_key(key, uuid):
auth_key = os.getenv('AUTH_KEY')
api_url = os.getenv('API_URL')
headers = {
'auth': auth_key,
'key': key,
'uuid': uuid
}
try:
response = requests.get(api_url, headers=headers)
if response.status_code == 200:
return True
except requests.exceptions.RequestException:
return False
return False
def main():
key = 'your_key_here'
uuid = 'your_uuid_here'
if ping('google.com'):
if check_key(key, uuid):
print('Key check successful.')
else:
print('Key check failed. Retrying...')
for attempt in range(3):
if ping('batchkeys.com'):
if check_key(key, uuid):
print('Key check successful on retry.')
break
else:
print(f'Attempt {attempt + 1} failed. Retrying...')
else:
print('Waiting and retrying ping to batchkeys.com...')
time.sleep(300) # Wait for 5 minutes before retrying
continue
else:
print('Key check failed after multiple attempts.')
else:
print('No internet connection.')
if __name__ == '__main__':
main()
Pinging too quickly or sending too many API requests in a short amount of time will rate limit your users so be mindful of how fast you send these pings/requests. See our documentation on staying within rate limits for more info.
Other Error Messages #
Our API sends detailed error messages back to your app in the form of JSON headers. If you received one of these messages you did not experience a connection error from BatchKeys. For a detailed list of error messages, please see our documentation on handling errors.