Creating API Requests
MagicalAPI provides two methods for handling API requests and receiving results. Choose the approach that best fits your workflow.
Two Ways to Get Results
1. Request ID (Polling)
The traditional approach where you poll for results:
- Initial Request: Send your data to get a
request_id - Status Check: Use the
request_idto poll for results - Final Response: Receive the processed data
Best for: Simple integrations, one-off requests, or when you need immediate control over when to check results.
2. Webhook (Push Notifications)
Receive results automatically without polling:
- Initial Request: Send your data with a
webhook_url - Automatic Delivery: Receive results at your endpoint when ready
Best for: Production applications, high-volume processing, or when you want to eliminate polling overhead.
For detailed webhook setup and usage, see the Webhook Integration Guide.
Choosing the Right Method
| Feature | Request ID | Webhook |
|---|---|---|
| Setup Complexity | Simple | Requires endpoint setup |
| API Calls | Multiple (polling) | Single request |
| Real-time Results | Manual polling needed | Automatic delivery |
| Best Use Case | Testing, simple scripts | Production, automation |
Request Flow (Request ID Method)
Step 1: Initial Request
Send your request to the appropriate service endpoint. You'll receive a 201 response with a request_id:
{
"data": {
"request_id": "abc123xyz"
},
"usage": {
"credits": 10
}
}
Request Parameters
Each service has specific parameters. For example:
- Profile Data: Requires
profile_name - Resume Parser: Needs
urlto the resume file - Company Data: Takes
company_name
See individual service documentation for parameter details.
Step 2: Retrieve Results
Use the request_id to check the status and get results by sending a request to the same service endpoint you initially used:
{
"request_id": "abc123xyz"
}
Polling Best Practices
- Poll Interval: Check every 2-3 seconds
- Timeout: Set a maximum wait time
- Error Handling: Implement retry logic
Example polling implementation:
import time
import requests
def get_results(service_endpoint, request_id, max_attempts=10):
headers = {
'Content-Type': 'application/json',
'api-key': 'YOUR-API-KEY'
}
for attempt in range(max_attempts):
response = requests.post(
service_endpoint, # Use the same endpoint as the initial request
headers=headers,
json={'request_id': request_id}
)
if response.status_code == 200:
return response.json()
time.sleep(2) # Wait 2 seconds between attempts
raise TimeoutError("Result not available after maximum attempts")
Credit Usage
The usage field in responses shows credit consumption:
{
"usage": {
"credits": 10
}
}
Monitor your credit balance:
- Check the MagicalAPI Panel
- Review costs on the Pricing Page
Example Workflows
Using Request ID
- Send Initial Request
curl -X POST "https://gw.magicalapi.com/profile-data" \
-H "Content-Type: application/json" \
-H "api-key: YOUR-API-KEY" \
-d '{"profile_name": "williamhgates"}'
- Check Status (using the same endpoint)
curl -X POST "https://gw.magicalapi.com/profile-data" \
-H "Content-Type: application/json" \
-H "api-key: YOUR-API-KEY" \
-d '{"request_id": "abc123xyz"}'
Using Webhook
Send a single request with your webhook URL:
curl -X POST "https://gw.magicalapi.com/profile-data" \
-H "Content-Type: application/json" \
-H "api-key: YOUR-API-KEY" \
-d '{
"profile_name": "williamhgates",
"webhook_url": "https://yourdomain.com/api/webhook"
}'
Results will be automatically sent to your webhook endpoint when ready. Learn more about setting up webhooks.
Use the Python SDK or Postman Collection to simplify request handling.
Error Handling
Common issues to handle:
- Invalid parameters
- Insufficient credits
- Timeout errors
See Status Codes for error handling guidance.
Related Resources
- Webhook Integration - Set up automatic result delivery
- Authentication Guide
- Status Codes
- API Overview