malte.huener
malte.huener2w ago

download / grab PDF from supabase storage and send it to an API

Hi there, i have a supabase trigger on inserts. this trigger gives back a pdf path and id. now i will send the id and binary pdf file to an API. How i should do this? { "schema": "public", "type": "INSERT", "old_record": null, "record": { "branch_name": "Gastronomie", "pdf_name": "https://coodsldfgelifogiqfdd.supabase.co/storage/v1/object/public/analyses_bucket/pdf/1727406193189000.pdf", "company": "wird ausgelesen..", "id": 1210, "status": "wird erstellt..", "owner_uuid": "dac193c1-1d68-4cc1-a75f-4f19b970c22b", "created_at": "2024-09-27T03:03:15.825172+00:00" }, "table": "analyses" }
3 Replies
AI Support
AI Support2w ago
AI Support Bot Information
<@1237643762801315881> you can react on the relevant answer (message) with a ✅ in this thread when you think it has been solved, by the bot or by a human! Anyone can react on this message with a ❌ if the GPT bot is unhelpful or hallucinating answers. Please note: Team members will review and answer the questions on best efforts basis.
AI Support
AI Support2w ago
AI Response
To achieve your goal of sending the ID and binary PDF file to an API using a Supabase trigger, you can follow these general steps: 1. Setup Supabase Trigger: Ensure that your trigger is correctly set to capture the INSERT events. 2. Download the PDF: Use the URL provided in the Supabase record to download the PDF file. 3. Send the Data to API: Utilize an HTTP request to send the ID and the PDF file to the desired API. Here is a step-by-step outline in more detail: 1. Supabase Trigger Setup Configure your trigger to execute a function upon an INSERT event. This function will handle the download and subsequent API request. 2. Download the PDF The function can download the PDF from the given URL. Here is a rough outline: - Extract the pdf_name (URL) and id from the inserted record. - Utilize a library (like requests in Python or similar in other languages) to download the PDF. 3. Send the Data to API Lastly, send the ID and the binary PDF file to the API: - Construct a multipart/form-data payload containing the ID and the binary PDF. - Send an HTTP POST request to your API endpoint. Example (Python)
import requests
from supabase import create_client, Client

# Supabase initialization
url = "your_supabase_url"
key = "your_supabase_key"
supabase: Client = create_client(url, key)

def handle_insert(event):
record = event["record"]
pdf_url = record["pdf_name"]
pdf_id = record["id"]

# Download the PDF
response = requests.get(pdf_url)
if response.status_code == 200:
pdf_data = response.content

# Send ID and PDF to the API
api_url = "your_api_endpoint"
files = {
'file': ('report.pdf', pdf_data, 'application/pdf'),
'id': (None, str(pdf_id))
}
api_response = requests.post(api_url, files=files)

if api_response.status_code == 200:
print("Successfully sent the PDF and ID to the API.")
else:
print(f"Failed to send data to the API. Status code: {api_response.status_code}")
else:
print(f"Failed to download PDF. Status code: {response.status_code}")

# Example event data as provided
event_data = {
"schema": "public",
"type": "INSERT",
"old_record": None,
"record": {
"branch_name": "Gastronomie",
"pdf_name": "https://coodsldfgelifogiqfdd.supabase.co/storage/v1/object/public/analyses_bucket/pdf/1727406193189000.pdf",
"company": "wird ausgelesen..",
"id": 1210,
"status": "wird erstellt..",
"owner_uuid": "dac193c1-1d68-4cc1-a75f-4f19b970c22b",
"created_at": "2024-09-27T03:03:15.825172+00:00"
},
"table": "analyses"
}

handle_insert(event_data)
import requests
from supabase import create_client, Client

# Supabase initialization
url = "your_supabase_url"
key = "your_supabase_key"
supabase: Client = create_client(url, key)

def handle_insert(event):
record = event["record"]
pdf_url = record["pdf_name"]
pdf_id = record["id"]

# Download the PDF
response = requests.get(pdf_url)
if response.status_code == 200:
pdf_data = response.content

# Send ID and PDF to the API
api_url = "your_api_endpoint"
files = {
'file': ('report.pdf', pdf_data, 'application/pdf'),
'id': (None, str(pdf_id))
}
api_response = requests.post(api_url, files=files)

if api_response.status_code == 200:
print("Successfully sent the PDF and ID to the API.")
else:
print(f"Failed to send data to the API. Status code: {api_response.status_code}")
else:
print(f"Failed to download PDF. Status code: {response.status_code}")

# Example event data as provided
event_data = {
"schema": "public",
"type": "INSERT",
"old_record": None,
"record": {
"branch_name": "Gastronomie",
"pdf_name": "https://coodsldfgelifogiqfdd.supabase.co/storage/v1/object/public/analyses_bucket/pdf/1727406193189000.pdf",
"company": "wird ausgelesen..",
"id": 1210,
"status": "wird erstellt..",
"owner_uuid": "dac193c1-1d68-4cc1-a75f-4f19b970c22b",
"created_at": "2024-09-27T03:03:15.825172+00:00"
},
"table": "analyses"
}

handle_insert(event_data)
Make sure to replace placeholder values like your_supabase_url, your_supabase_key, and your_api_endpoint with actual values. Adapt the code as necessary depending on the language and framework you are using. For further reading and tutorials on building workflows involving Supabase triggers, check the BuildShip documentation: - BuildShip Supabase Trigger【4:7†source】. - General Workflow Tutorial【4:7†source】. If you need any specific help with BuildShip configurations, don't hesitate to send a support request via your BuildShip Dashboard.
BuildShip AI Support
malte.huener
malte.huener2w ago
how to implement the python code into a node?