API Request Tool
The API Request tool allows your agents to interact with any REST API endpoint. It provides complete control over HTTP methods, headers, request bodies, and response handling.This tool has Default status, meaning it’s production-ready and available on all subscription plans.
Overview
The API Request tool is one of the most versatile tools in PLai Framework, enabling agents to:Connect to APIs
Integrate with any REST API service or endpoint
Custom Headers
Configure authentication, content types, and custom headers
Flexible Methods
Support for GET, POST, PATCH, DELETE, and LIST HTTP methods
Template System
Dynamic URL and body templating with variable substitution
Configuration Parameters
The API endpoint URL with optional template variables
Example:
Variables: Use
Example:
https://api.example.com/users/{{user_id: str}}/profile
Variables: Use
{{variable_name: type}} syntax (types: str, int, float, bool)HTTP method for the request
Supported methods:
Note: This parameter is required (no default value)
Supported methods:
GET, POST, PATCH, DELETE, LIST
Note: This parameter is required (no default value)
HTTP headers as a JSON string with optional template variables
Example:
Default:
Note: Must be a JSON string, not an object
Example:
"{\"Authorization\": \"Bearer {{api_token: str}}\", \"User-Agent\": \"PLai-Agent/1.0\"}"
Default:
"{}"
Note: Must be a JSON string, not an object
Content type for request body
Options:
Options:
application/json- For JSON payloads (default)application/x-www-form-urlencoded- For form data
Default:application/json
Request body as a JSON string or form-encoded string with template variables
Example (JSON):
Example (Form):
Default:
Note: Must be a string. For JSON, system adds quotes automatically for str types
Example (JSON):
"{\"name\": {{user_name: str}}, \"email\": {{user_email: str}}}"
Example (Form):
"username={{username: str}}&password={{password: str}}"
Default:
"{}"
Note: Must be a string. For JSON, system adds quotes automatically for str types
Setup Instructions
Set Content Type (Optional)
Choose the appropriate content type for your API (default:
application/json)Required Parameters:
name, description, method, url_template, body_template, headers_templateOptional Parameters: content_type (defaults to application/json)Usage Examples
Basic GET Request
Authenticated API Call
POST Request with Data
CRM Integration (Salesforce)
Common Use Cases
Customer Relationship Management (CRM)
- Salesforce
- HubSpot
- Create/update leads and contacts
- Retrieve customer information
- Update opportunity status
- Sync customer data
Payment Processing
- Stripe
- PayPal
- Process payments
- Manage customer subscriptions
- Handle refunds
- Retrieve transaction history
Communication Services
- Slack
- Discord
- Send messages to channels
- Create private conversations
- Post file attachments
- Manage workspace users
Template Variables
Variable Substitution
The API Request tool uses a typed variable system with the format{{variable_name: type}}:
Supported Variable Types
str
String values - text, IDs, names, etc.
int
Integer numbers - counts, IDs, quantities
float
Decimal numbers - prices, percentages, measurements
bool
Boolean values - true/false flags
- ✅ Always include the type:
{{variable: str}} - ✅ Templates must be JSON strings, not objects
- ✅ For
strtypes in JSON, quotes are added automatically - ✅ All templates are required:
url_template,body_template,headers_template
Variable Type Examples
Available Variables
Variables are provided by the agent during execution:- User-provided: Variables from chat context or form inputs
- System variables: Current time, agent name, user ID, etc.
- Previous responses: Data from earlier tool calls in the conversation
- Context variables: Information from the current conversation
Real-World Examples
GET Request with URL Parameters
POST Request with JSON Body
POST Request with Form-Encoded Data
Response Handling
Response Processing
The API Request tool processes API responses and returns them as strings:- Status Code Validation: Automatically validates HTTP status codes using
raise_for_status() - JSON Parsing: Attempts to parse JSON responses
- String Conversion: Returns response data as a string representation
- Error Handling: Returns error messages as strings when requests fail
Response Format
Successful JSON Response:The agent will receive the response as a string and can extract information from it. The response includes the actual API data when JSON parsing is successful, or a success message when the response body cannot be parsed as JSON.
Error Handling
Error Response Behavior
When an API request fails, the tool returns an error message as a string. All errors are caught and returned as descriptive text messages.Common Error Scenarios
Authentication Errors (401/403)
Authentication Errors (401/403)
Cause: Invalid or expired API keys, insufficient permissionsError Message Example:Solutions:
- Verify API key is correct and active
- Check permission scopes for the API key
- Ensure proper header format for authentication
- Confirm the API endpoint supports your authentication method
Rate Limiting (429)
Rate Limiting (429)
Cause: Exceeded API rate limitsError Message Example:Solutions:
- Reduce request frequency
- Wait before retrying the request
- Upgrade API plan if available
Bad Request (400)
Bad Request (400)
Cause: Invalid request format or missing required fieldsError Message Example:Solutions:
- Validate request body against API documentation
- Check required fields are included
- Verify data types match API expectations
- Test with API documentation examples
Network Errors
Network Errors
Cause: Connection issues, DNS failures, or timeoutError Message Example:Solutions:
- Check network connectivity
- Verify the API endpoint URL is correct
- Check if the API service is operational
- Ensure firewall rules allow the connection
All errors are logged and returned as string messages. The tool uses basic error handling provided by the requests library, with automatic status code validation via
raise_for_status().Security Best Practices
Secure Configuration
Remember that
headers_template must be a JSON string, not an object.Security Features
The API Request tool provides basic security through the underlying HTTP libraries:- HTTPS Support: ✅ Fully supported for secure connections
- SSL Certificate Validation: ✅ Enabled by default via the requests library
- Secure Headers: ✅ Support for Authorization headers and API keys via templates
Security Checklist
- Use HTTPS: Always use secure HTTPS endpoints
- Variable Substitution: Use template variables for sensitive data like API keys
- Minimal Permissions: Use API keys with minimal required permissions
- Rotate Keys: Regularly rotate API keys and tokens
- Monitor Usage: Track API usage patterns in your application
Troubleshooting
Testing Your Configuration
Use these approaches to test your API Request tool configuration:- API Testing Tools: Test your endpoint with Postman, Insomnia, or curl first
- Verify JSON Format: Ensure
headers_templateandbody_templateare valid JSON strings - Check Variable Types: Verify all template variables have correct type annotations
- Test Incrementally: Start with simple GET requests before adding complex bodies
- Review Responses: Check the string response returned by the tool for debugging
Common Issues
Template Parsing Errors
Template Parsing Errors
Cause: Invalid JSON in
headers_template or body_templateSolution: Ensure templates are valid JSON strings with proper escaping:Missing Variable Values
Missing Variable Values
Cause: Template variable not provided during executionSolution: Ensure all variables in templates are available from the agent context
Type Conversion Errors
Type Conversion Errors
Cause: Variable value cannot be converted to specified typeSolution: Verify the data type matches the template variable type:
{{count: int}}requires integer value{{active: bool}}requires boolean value