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.
Here’s a simple example of using Python to connect to an external API within Machine:
Copy
import requests# Make a GET request to a public APIresponse = requests.get('https://api.example.com/data')# Check if the request was successfulif 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.
import requestsdata = { '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}")