Use Python REST APIs for industrial automation with requests. Send plant data with GET/POST, JSON payloads, timeouts, authentication headers and robust error handling.
Architecture: Python REST API Integration
Python REST API Integration
Plant / SQL Data
Build application payload
dictPython requests
Create HTTP request
requests.postREST Endpoint
HTTPS URL
/api/eventsServer
Validate/process request
API serviceResponse
Status + JSON
response.json()Logging
Record success/failure
logger.info/error1. Install requests
python -m pip install requests2. GET data from an API
import requests response = requests.get( "https://api.example.com/status", timeout=10,
)
response.raise_for_status()
print(response.json())3. POST plant data as JSON
payload = { "sqf_no": 2, "charge_no": "CHG-001", "temp_act": 850.2,
} response = requests.post( "https://api.example.com/events", json=payload, timeout=10,
)
response.raise_for_status()4. Add an authorization header
headers = { "Authorization": "Bearer YOUR_TOKEN", "Accept": "application/json",
}
response = requests.get(url, headers=headers, timeout=10)5. Handle timeout and HTTP errors
import requests try: response = requests.get(url, timeout=10) response.raise_for_status()
except requests.Timeout: print("API timeout")
except requests.RequestException as error: print("API request failed:", error)6. REST integration checklist
- Use HTTPS for external/sensitive traffic
- Always set timeouts
- Validate JSON fields and engineering units
- Handle HTTP status codes and retries deliberately
- Store credentials/tokens securely
Frequently Asked Questions
What is a REST API in industrial automation?
It is an HTTP-based interface that lets authorized systems exchange structured data, commonly JSON, between plant, enterprise and cloud applications.
Why should Python requests have a timeout?
Without a timeout, a network call can wait indefinitely and block an automation/reporting workflow.
Can REST APIs replace OPC UA?
They serve different integration patterns. OPC UA is designed for industrial interoperability and information models; REST is common for web/enterprise application integration.
