Using External APIs with Machine

Machine can connect to virtually any external API using its built-in Python and command-line capabilities. This guide explains how to use these features to expand Machine’s functionality by connecting to external services.

API Integration Capabilities

Python Requests

Machine can execute Python code that uses the requests library to call external APIs.

Curl Commands

For direct HTTP requests, Machine supports curl commands for quick API interactions.

API Authentication

Securely connect to APIs that require authentication using various methods.

Data Processing

Parse and transform API responses directly within Machine.

Basic Python API Example

Here’s a simple example of using Python to connect to an external API within Machine:

import requests

# Make a GET request to a public API
response = requests.get('https://api.example.com/data')

# Check if the request was successful
if response.status_code == 200:
    # Process the JSON response
    data = response.json()
    print(f"Retrieved {len(data)} records")
    
    # You can now use this data in your workflow
    for item in data[:5]:  # Show first 5 items
        print(item['name'])
else:
    print(f"Error: {response.status_code}")

Simply paste this code into Machine, adjust the URL and parsing logic for your specific API, and run it.

Using Curl Commands

If you prefer using curl commands, Machine supports those as well:

curl -X GET https://api.example.com/data -H "Authorization: Bearer YOUR_TOKEN"

Authentication Methods

Machine supports various authentication methods for APIs:

API Key Authentication

import requests

headers = {
    'X-API-Key': 'your_api_key_here'
}

response = requests.get('https://api.example.com/data', headers=headers)

OAuth Authentication

import requests

headers = {
    'Authorization': 'Bearer your_oauth_token_here'
}

response = requests.get('https://api.example.com/data', headers=headers)

Basic Authentication

import requests

response = requests.get(
    'https://api.example.com/data',
    auth=('username', 'password')
)

POST Requests with Data

Sending data to an API:

import requests

data = {
    'name': 'New Item',
    'description': 'This is a new item created via API'
}

response = requests.post('https://api.example.com/items', json=data)

if response.status_code == 201:
    print("Item created successfully!")
    print(response.json())
else:
    print(f"Error: {response.status_code}")

Common API Use Cases

Best Practices

When using external APIs with Machine, follow these best practices:

  1. Security: Never hardcode sensitive credentials directly in your code. Consider using environment variables or secure storage.

  2. Rate Limiting: Be aware of API rate limits. Add delays between requests if making multiple calls to avoid being blocked.

  3. Error Handling: Always implement proper error handling to manage API failures gracefully.

  4. Data Validation: Validate the data received from external APIs before using it in critical operations.

  5. Caching: Consider caching API responses for frequently accessed data to improve performance.

Limitations and Considerations

  • Machine has network connectivity to most common APIs but may have restrictions for security reasons
  • Some APIs may have CORS or other security restrictions that prevent direct access
  • For very large datasets, consider pagination or streaming techniques
  • Machine operates within its security sandbox, so some system-level operations might be restricted

Remember that when using external APIs, you’re responsible for complying with each service’s terms of use and data handling requirements.