Building and Consuming REST APIs for IoT and Monitoring Systems
A practical guide to designing, building, and consuming REST APIs — for IoT data ingestion, NMS integration, device management, and real-time monitoring dashboards.
Why APIs Matter for IoT and Monitoring
APIs (Application Programming Interfaces) are the glue that connects IoT devices, monitoring systems, dashboards, and external services. A well-designed API enables:
- IoT devices to send sensor data to a central server
- Dashboards to query real-time and historical data
- External systems to integrate with your monitoring platform
- Mobile apps to control devices remotely
- Automation scripts to trigger actions based on conditions
REST API Fundamentals
REST (Representational State Transfer) is the most common API architecture for web services.
Core Principles
- Stateless — Each request contains all information needed (no server-side sessions)
- Resource-based — URLs represent resources (nouns, not verbs)
- HTTP methods — Use standard methods for operations
- JSON responses — Standard data format
HTTP Methods
| Method | Operation | Example | Idempotent | |--------|-----------|---------|-----------| | GET | Read | Get sensor data | Yes | | POST | Create | Submit new reading | No | | PUT | Update (full) | Update device config | Yes | | PATCH | Update (partial) | Update one field | Yes | | DELETE | Remove | Delete a device | Yes |
URL Design
# Good URL design (resource-based)
GET /api/v1/devices # List all devices
GET /api/v1/devices/123 # Get device 123
POST /api/v1/devices # Create new device
PUT /api/v1/devices/123 # Update device 123
DELETE /api/v1/devices/123 # Delete device 123
GET /api/v1/devices/123/readings # Get readings for device 123
POST /api/v1/devices/123/readings # Submit new reading
# Filtering, sorting, pagination
GET /api/v1/readings?device=123&from=2026-01-01&to=2026-02-01
GET /api/v1/devices?status=active&sort=name&page=2&limit=20
HTTP Status Codes
| Code | Meaning | Use | |------|---------|-----| | 200 | OK | Successful GET, PUT, PATCH | | 201 | Created | Successful POST | | 204 | No Content | Successful DELETE | | 400 | Bad Request | Validation error | | 401 | Unauthorized | Missing or invalid auth | | 403 | Forbidden | Authenticated but not authorized | | 404 | Not Found | Resource doesn't exist | | 409 | Conflict | Duplicate resource | | 429 | Too Many Requests | Rate limit exceeded | | 500 | Server Error | Unexpected error |
Building an API: Node.js + Express
Project Setup
Basic API Server
API Authentication
API Key Authentication
Simple and suitable for IoT devices:
JWT (JSON Web Token) Authentication
For user-facing dashboards:
Consuming APIs
From IoT Devices (Python)
From JavaScript (Dashboard)
Using curl (Testing)
Best Practices
Rate Limiting
Input Validation
API Versioning
Always version your API to allow backward-compatible changes:
/api/v1/devices # Current version
/api/v2/devices # Future version with breaking changes
Error Response Format
Consistent error responses make debugging easier:
Conclusion
REST APIs are the standard way to connect IoT devices, monitoring systems, and dashboards. Whether you're sending sensor readings from an Arduino, querying data for a dashboard, or integrating with third-party services, a well-designed API makes it all possible. Focus on clear URL design, proper authentication, input validation, and rate limiting to build APIs that are both powerful and secure.
Related: WebSocket for Real-Time Dashboards, IoT Real-Time Data Transfer, and SNMP Monitoring.