Built-in Utility Functions
Based provides built-in utility functions that are available in all deployments for common operations like debugging, notifications, and more.
Print Line Debugging
The print function works like Python’s standard print, but outputs are captured and made available in the session trace for debugging purposes. This is invaluable for understanding flow execution and troubleshooting issues.
Syntax:
Usage:
# Print simple messages for debugging
print("Starting order processing...")
# Print variable values to inspect state
order_data = response.ask(
question="Extract order details",
example={"items": [], "total": 0}
)
print("Extracted order:", order_data)
# Print within conditional logic to trace execution path
if order_data["total"] > 100:
print("High-value order detected, applying discount")
discount = order_data["total"] * 0.1
print(f"Discount amount: ${discount}")
All print outputs appear in the session trace view, making it easy to debug conversation flows without interrupting the user experience. Print statements do not send messages to the user—they’re purely for developer debugging.
Sending SMS Messages with send_sms
The send_sms function allows you to send SMS messages programmatically from within your Based flow. This is useful for sending confirmations, notifications, or follow-up messages.
Syntax:
result = await send_sms(
to="recipient_phone_number",
content="message_content",
from_number="your_phone_number"
)
Parameters:
to (string, required): The recipient’s phone number in E.164 format (e.g., "+12025551234")
content (string, required): The SMS message content to send
from_number (string, required): Your phone number from your Brainbase phone number library
Return Value:
Returns an SMSResult object with the following properties:
| Property | Type | Description |
|---|
success | boolean | True if SMS was sent successfully |
status | string | Status code: "sent", "failed", "skipped", or "error" |
message_sid | string | Twilio message SID (if sent successfully) |
to | string | Recipient phone number |
from_number | string | Sender phone number |
error | string | Error message (if failed) |
error_code | string | Provider error code (if applicable) |
Example:
# Send an order confirmation SMS
result = await send_sms(
to=customer_phone,
content=f"Your order #{order_id} has been confirmed! Estimated delivery: {delivery_time}",
from_number="+15551234567"
)
if result.success:
print(f"SMS sent successfully: {result.message_sid}")
say("I've sent a confirmation to your phone!", exact=True)
else:
print(f"SMS failed: {result.error}")
say("I wasn't able to send an SMS confirmation, but your order is still confirmed.", exact=True)
Handling Failures Gracefully:
The send_sms function is designed to never interrupt your flow. All errors are captured in the result object, allowing you to handle failures gracefully:
# Example: Appointment reminder with fallback
result = await send_sms(
to=patient_phone,
content=f"Reminder: Your appointment with Dr. {doctor_name} is tomorrow at {appointment_time}",
from_number="+15559876543"
)
if result.success:
say("I've sent you an SMS reminder for your appointment.", exact=True)
elif result.status == "skipped":
# SMS was skipped (e.g., A2P verification issue)
print(f"SMS skipped: {result.error}")
say("I'll make sure to remind you about your appointment.", exact=True)
else:
# SMS failed for another reason
print(f"SMS error: {result.error}")
say("Your appointment is confirmed. Please make a note of the time.", exact=True)
The from_number must be a phone number registered in your Brainbase phone number library with proper A2P (Application-to-Person) verification for SMS delivery compliance.
Making HTTP Requests
Based provides the standard Python requests module for making HTTP requests to external APIs. This gives you full compatibility with the widely-used requests library.
Examples:
# GET request
response = requests.get(
url="https://api.example.com/users/123",
headers={"Authorization": "Bearer your-token"}
)
if response.ok:
user_data = response.json()
print(f"User: {user_data['name']}")
# POST request with JSON body
response = requests.post(
url="https://api.example.com/orders",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer your-token"
},
json={
"items": order_items,
"customer_id": customer_id
}
)
if response.ok:
order = response.json()
say(f"Order {order['id']} created successfully!", exact=True)
else:
print(f"Request failed: {response.status_code} - {response.text}")
# Other HTTP methods
response = requests.put(url, headers=headers, json=data)
response = requests.delete(url, headers=headers)
response = requests.patch(url, headers=headers, json=data)
Using .ask() with API Responses:
You can use the .ask() method on response data to extract structured information:
response = requests.get("https://api.example.com/products")
products = response.json()
# Extract specific fields using .ask()
summary = products.ask(
question="Extract the top 3 products by price with name and price",
example={"products": [{"name": "Widget", "price": 29.99}]}
)
All HTTP requests are automatically logged in the session trace for debugging and observability. You can see request/response details, timing, and any errors in the trace view.
Legacy API Utility (Deprecated)
The api utility is deprecated. Please use the standard requests module instead.
The legacy api.get_req() and api.post_req() methods are still supported for backwards compatibility:
# Deprecated - use requests.get() instead
result = api.get_req(url='https://api.example.com/data', headers={...})
# Deprecated - use requests.post() instead
result = api.post_req(url='https://api.example.com/data', headers={...}, body={...})
Third-Party Integrations
Based provides an integrations client for connecting to third-party services configured in your Brainbase workspace.
Usage:
# Call an integration action (app_name.action_name pattern)
result = await integrations.slack.send_message(
channel="#notifications",
text=f"New order received: {order_id}"
)
# Send email via Gmail
result = await integrations.gmail.send_email(
to="[email protected]",
subject="Order Confirmation",
body=f"Your order #{order_id} has been confirmed!"
)
Integrations must be connected in your Brainbase workspace before they can be used in Based flows. See the Integrations documentation for setup instructions.