Overview
The REST API is implemented in theapi component and provides endpoints for:
- Checking API status
- Reading and setting entity states
- Firing events
- Calling services
- Retrieving configuration information
- Streaming events
homeassistant/components/api/__init__.py
Authentication
All API requests (except for the status endpoint) require authentication. Include a long-lived access token in theAuthorization header:
You can create long-lived access tokens in the Home Assistant UI under your user profile.
Base URL
All API endpoints are prefixed with/api. The base URL constants are defined in homeassistant/const.py:
URL_API= “/api/”URL_API_STATES= “/api/states”URL_API_EVENTS= “/api/events”URL_API_SERVICES= “/api/services”
Core Endpoints
GET /api/
Check if the API is running. Response:GET /api/config
Get Home Assistant configuration. Requires: Authentication, Admin Response:State Endpoints
GET /api/states
Get all entity states. Requires: Authentication Response:GET /api/states/
Get state of a specific entity. Requires: Authentication Response:POST /api/states/
Update the state of an entity. Requires: Authentication Request Body:Event Endpoints
GET /api/events
Get all event types that are being listened to. Requires: Authentication Response:POST /api/events/
Fire an event. Requires: Authentication Request Body:Service Endpoints
GET /api/services
Get all available services. Requires: Authentication Response:POST /api/services//
Call a service. Requires: Authentication Request Body:Event Stream
GET /api/stream
Open a streaming connection for server-sent events. Requires: Authentication, Admin Query Parameters:restrict(optional): Comma-separated list of event types to receive
The stream automatically pings every 50 seconds to keep the connection alive.
Error Responses
The API returns standard HTTP status codes:200 OK: Request succeeded400 Bad Request: Invalid request format401 Unauthorized: Missing or invalid authentication token403 Forbidden: Insufficient permissions404 Not Found: Entity or endpoint not found500 Internal Server Error: Server error
Implementation Details
The REST API is implemented usingaiohttp views. Key views from homeassistant/components/api/__init__.py:
APIStatusView- API status checkAPICoreStateView- Core state retrievalAPIStatesView- All statesAPIEntityStateView- Single entity stateAPIEventListenersView- Event listenersAPIEventView- Fire eventsAPIServicesView- List all servicesAPIDomainServicesView- Services for a domainAPIEventStream- Server-sent event stream
HomeAssistantView and implements HTTP method handlers.
Best Practices
Use Long-Lived Tokens
Create dedicated tokens for API access, not user passwords
Handle Errors
Check HTTP status codes and handle error responses
Respect Rate Limits
Don’t overwhelm the API with excessive requests
Use WebSocket for Real-Time
For real-time updates, use the WebSocket API instead of polling
See Also
WebSocket API
Real-time bidirectional communication API
Service Calls
Learn more about calling services
State Machine
Understand the state management system
Event System
Deep dive into the event bus