Skip to content

Instantly share code, notes, and snippets.

@brunovcosta
Created January 3, 2026 00:49
Show Gist options
  • Select an option

  • Save brunovcosta/9a7ee776d4e63e1e8ff8408baac34e1f to your computer and use it in GitHub Desktop.

Select an option

Save brunovcosta/9a7ee776d4e63e1e8ff8408baac34e1f to your computer and use it in GitHub Desktop.
Export SQL with some tables as API
import abstra.hooks as ah
from abstra.tasks import send_task
from abstra.tables import run_sql
from abstra_json_sql.authorization import Permissions
import os
# Use Abstra Hooks to create Python endpoints
body, _ , headers = ah.get_request()
cloud_api = Permissions()
cloud_api.grant("SELECT", "customers")
cloud_api.grant("SELECT", "accounts")
billing = Permissions()
billing.grant("SELECT", "customers")
billing.grant("SELECT", "accounts")
admin = Permissions()
admin.grant("SELECT", "customers")
admin.grant("SELECT", "accounts")
admin.grant("INSERT", "accounts")
tokens = {
os.environ["CLOUD_API_TABLES_API_TOKEN"]: cloud_api,
os.environ["BILLING_TABLES_API_TOKEN"]: billing,
os.environ["ADMIN_TABLES_API_TOKEN"]: admin
}
token = headers.get("authorization").replace("Bearer ", "")
if token not in tokens:
ah.send_response(status_code=401)
exit()
permissions = tokens[token]
if not isinstance(body, dict) or "query" not in body:
ah.send_response(status_code=400)
exit()
sql_query = body["query"]
if not permissions.allowed(sql_query):
ah.send_response(status_code=401)
exit()
ah.send_json(run_sql(sql_query))
@brunovcosta
Copy link
Author

In this case, the customer can just POST https://...abstra.app/_hooks/... with body { "query": " ... " } and it will restrict which queries the user can call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment