Skip to main content
The Home Assistant REST API allows you to interact with a Home Assistant instance using HTTP requests. The API is served over the HTTP component on the same port as the web interface (default: 8123).

Overview

The REST API is implemented in the api component and provides endpoints for:
  • Checking API status
  • Reading and setting entity states
  • Firing events
  • Calling services
  • Retrieving configuration information
  • Streaming events
Source: homeassistant/components/api/__init__.py

Authentication

All API requests (except for the status endpoint) require authentication. Include a long-lived access token in the Authorization 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:
Example:

GET /api/config

Get Home Assistant configuration. Requires: Authentication, Admin Response:

State Endpoints

GET /api/states

Get all entity states. Requires: Authentication Response:
Example:

GET /api/states/

Get state of a specific entity. Requires: Authentication Response:
Example:

POST /api/states/

Update the state of an entity. Requires: Authentication Request Body:
Response: Returns the new state object. Example:

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:
Response:
Example:

Service Endpoints

GET /api/services

Get all available services. Requires: Authentication Response:

POST /api/services//

Call a service. Requires: Authentication Request Body:
Response:
Example:

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
Example:
The stream sends periodic ping messages and event data:
The stream automatically pings every 50 seconds to keep the connection alive.

Error Responses

The API returns standard HTTP status codes:
  • 200 OK: Request succeeded
  • 400 Bad Request: Invalid request format
  • 401 Unauthorized: Missing or invalid authentication token
  • 403 Forbidden: Insufficient permissions
  • 404 Not Found: Entity or endpoint not found
  • 500 Internal Server Error: Server error
Error Response Format:

Implementation Details

The REST API is implemented using aiohttp views. Key views from homeassistant/components/api/__init__.py:
  • APIStatusView - API status check
  • APICoreStateView - Core state retrieval
  • APIStatesView - All states
  • APIEntityStateView - Single entity state
  • APIEventListenersView - Event listeners
  • APIEventView - Fire events
  • APIServicesView - List all services
  • APIDomainServicesView - Services for a domain
  • APIEventStream - Server-sent event stream
Each view extends 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