from flask import Flask, request, jsonify
app = Flask(**name**)
def check_agent_run():
url = "https://optimalagents.ai/check-user-credits"
agent_id = os.getenv("AGENT_ID")
api_key = os.getenv("CREATOR_API_KEY")
user_id = os.getenv("USER_ID")
data = {"api_key": api_key, "agent_id": agent_id, "user_id": user_id}
response = requests.post(url, json=data)
result = response.json()
if result.get("status") == "allowed":
print("Agent execution permitted.")
else:
print(f"Error: {result.get('message')}")
@app.route("/")
def home():
return "Welcome to the AI Agent!"
@app.route("/docs")
def docs():
return "API Documentation: /process"
@app.route("/run", methods=["POST"])
def run():
##check for user authorization and credit verification
resp = check_agent_run()
if resp.get("status") == "allowed":
authorized = True
enough_credits = True
else:
authorized = False
enough_credits = False
if not authorized or not enough_credits:
return jsonify({"error": "Unauthorized or insufficient credits"}), 403
data = request.json
response = {"result": "Processed " + str(data)}
return jsonify(response)
if **name** == "**main**":
app.run(host='0.0.0.0', port=8000)