M O O N I T O

Integrate Analytics API with your Python Application (e.g., Flask, Django or FastAPI)

Here is the documentation guide on integrating Analytics API into Python applications (e.g., Flask), we'll use the requests library for making HTTP requests in the browser.

  • First, sign up for the domain you want to protect and learn about its visitors on the Analytics page.

Add New Domain

  • Then, install the requests library.
pip install requests
  • Now, you can use the following Python code.
from flask import Flask, request
import requests

app = Flask(__name__)

# Configuration start
api_public_key = 'Your API Public Key'  # Put your API Public Key here
api_secret_key = 'Your API Secret Key'  # Put your API Secret Key here
# Configuration end

# Assuming you are using Flask, if not, adjust the request object accordingly
@app.before_request
def before_request():
    # Client IP Address Retrieval
    client_ip = (
        request.headers.get('CF-Connecting-IP')
        or request.headers.get('X-Forwarded-For')
        or request.remote_addr
    )

    try:
        # Construct URL for API request
        api_url = f'https://moonito.net/api/v1/analytics?ip={client_ip}&ua={request.headers.get("User-Agent")}&events={request.path}&domain={request.host.lower()}'

        # Set up request headers
        headers = {
            'User-Agent': 'Mozilla/5.0 (Linux; Android 13; SM-G991U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36',
            'X-Public-Key': api_public_key,
            'X-Secret-Key': api_secret_key,
        }

        # Execute API request
        response = requests.get(api_url, headers=headers)
        result = response.json()

        # Process data as needed
        if result['data']['status']['need_to_block']:
            # Do some action when a visitor is detected as "need_to_block"
            # For example, return 403 Forbidden
            return '', 403
    except Exception as error:
        print('Error:', error)

# Rest of your Flask application setup goes here
if __name__ == '__main__':
    app.run(debug=True)
  • Input your Secret and Public API Key, then save.
api_public_key = 'Your API Public Key'  # Put your API Public Key here
api_secret_key = 'Your API Secret Key'  # Put your API Secret Key here
  • Edit response handling, the code processes the API response, and if the status indicates the need to block the visitor, it takes action (e.g., return 403 Forbidden).
# Process data as needed
if result['data']['status']['need_to_block']:
  # Do some action when a visitor is detected as "need_to_block"
  # For example, return 403 Forbidden
  return '', 403
  • This example assumes you're using Flask. If you are using Django or FastAPI, you need to adjust the code accordingly. Also, make sure your application has the necessary permissions to make outbound requests.
  • Do some testing. Open your Flask application on a web browser. This Python code will run when the page loads, requesting the analytics endpoint.
  • For information regarding visitor statistics, you can go to the Analytics page for details.