Skip to content

buddyh/alexa-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alexa-cli

A command-line interface for controlling Amazon Alexa devices using the unofficial Alexa API.

Control your smart home from the terminal. Turn lights on/off, adjust thermostats, lock doors, play music, make announcements - anything you can say to Alexa, you can script.

Installation

Homebrew (macOS/Linux)

brew install buddyh/tap/alexacli

Go

go install github.com/buddyh/alexa-cli/cmd/alexa@latest

Build locally

git clone https://github.com/buddyh/alexa-cli
cd alexa-cli
make build
./bin/alexacli --help

Authentication

This CLI uses Amazon's unofficial API (the same one the Alexa app uses). You'll need to obtain a refresh token using the alexa-cookie-cli tool.

Step 1: Get your refresh token

Download alexa-cookie-cli from the releases page, then:

# For US Amazon accounts
./alexa-cookie-cli --amazonPage amazon.com --baseAmazonPage amazon.com --amazonPageProxyLanguage en_US --acceptLanguage en-US

This opens a local proxy at http://127.0.0.1:8080. Log into your Amazon account there, and the refresh token will be displayed.

Step 2: Configure alexa-cli

# Interactive
alexacli auth

# Direct
alexacli auth <your-refresh-token>

# Or set environment variable
export ALEXA_REFRESH_TOKEN=<your-token>

Configuration is stored in ~/.alexa-cli/config.json.

Usage

List Devices

# List all Echo devices
alexacli devices

# JSON output for scripting
alexacli devices --json

Text-to-Speech

# Speak on a specific device
alexacli speak "Hello world" -d "Kitchen Echo"

# Announce to ALL devices
alexacli speak "Dinner is ready!" --announce

# Device name matching is flexible
alexacli speak "Build complete" -d Kitchen
alexacli speak "Build complete" -d "Living Room"

Voice Commands (Smart Home Control)

Send any command as if you spoke it to Alexa. This is the primary way to control smart home devices:

# Smart home control - lights, switches, plugs
alexacli command "turn off the living room lights" -d Kitchen
alexacli command "turn on the porch light" -d Kitchen
alexacli command "dim the bedroom lights to 50 percent" -d Bedroom

# Thermostats and climate
alexacli command "set thermostat to 72 degrees" -d Bedroom
alexacli command "what's the temperature inside" -d Kitchen

# Locks and security
alexacli command "lock the front door" -d Kitchen

# Play music
alexacli command "play jazz music" -d "Living Room"
alexacli command "stop" -d "Living Room"

# Ask questions
alexacli command "what's the weather" -d Kitchen

# Timers and reminders
alexacli command "set a timer for 10 minutes" -d Kitchen

The -d flag specifies which Echo device processes the command. The device itself doesn't need to be near the smart home device - Alexa routes the command appropriately.

Ask (Get Response Back)

Send a command and capture Alexa's text response:

# Query and get the response
alexacli ask "what's the thermostat set to" -d Kitchen
# Output: The thermostat is set to 68 degrees.

alexacli ask "what's on my calendar today" -d Kitchen
# Output: You have 2 events today. First, standup at 9 AM...

# JSON output for parsing
alexacli ask "what time is it" -d Kitchen --json
# {"data":{"device":"Kitchen","question":"what time is it","response":"The time is 3:22 PM."},"success":true}

This retrieves Alexa's actual response by polling the voice activity history. Useful for:

  • Querying smart home device state
  • Getting Alexa-specific information (calendar, reminders, timers)
  • Verifying that commands worked

History

View recent voice activity:

alexacli history
alexacli history --limit 5
alexacli history --json

Shows what was said and what Alexa responded with.

Routines (Coming Soon)

# List available routines
alexacli routine list

# Execute a routine
alexacli routine run "Good Night"

Direct Smart Home API (Coming Soon)

For granular, programmatic control of smart home devices without natural language:

# List smart home devices with IDs and capabilities
alexacli sh list

# Direct device control by name
alexacli sh on "Kitchen Light"
alexacli sh off "All Lights"
alexacli sh brightness "Bedroom Lamp" 50

Note: For most use cases, especially AI agents, alexacli command is recommended. Natural language commands are more flexible and match how you'd interact with Alexa verbally. The direct API is useful when you need exact device IDs or want to avoid natural language parsing.

JSON Output

All commands support --json for machine-readable output:

alexacli devices --json | jq '.[].name'
alexacli speak "test" -d Kitchen --json

Command Reference

Command Description Status
alexacli devices List all Echo devices Working
alexacli speak <text> -d <device> Text-to-speech on device Working
alexacli speak <text> --announce Announce to all devices Working
alexacli command <text> -d <device> Voice command (smart home, music, etc.) Working
alexacli ask <text> -d <device> Send command, get response back Working
alexacli history View recent voice activity Working
alexacli auth Configure authentication Working
alexacli routine list List routines WIP
alexacli routine run <name> Execute routine WIP
alexacli sh list List smart home devices WIP
alexacli sh on/off <device> Control device WIP

Note: Routines and Direct Smart Home API are work-in-progress. Use alexacli command for smart home control - it's fully working and actually preferred for AI/agentic use since natural language is more flexible than device IDs.

Use Cases

Claude Code / AI Agent Integration

This CLI was built specifically to allow AI assistants to control smart home devices. The natural language command interface is ideal for agentic use - the AI can construct commands the same way a human would speak to Alexa:

# AI can control any smart home device using natural language
alexacli command "turn off all the lights" -d Kitchen
alexacli command "set the thermostat to 68" -d Kitchen
alexacli command "lock the front door" -d Kitchen

# Notifications and announcements
alexacli speak "The build finished successfully" -d Office
alexacli speak "Reminder: standup in 5 minutes" --announce

Scripting and Automation

#!/bin/bash
# Announce when a long-running job finishes
make build && alexacli speak "Build complete!" --announce

Home Automation

# Morning routine script
alexacli command "good morning" -d Bedroom
alexacli command "turn on kitchen lights" -d Kitchen
alexacli command "what's on my calendar today" -d Kitchen

Token Refresh

The refresh token is valid for approximately 14 days. If you get authentication errors, run alexacli auth again with a fresh token from alexa-cookie-cli.

Troubleshooting

"not configured" error

Run alexacli auth <token> to configure your refresh token.

Device not found

Use alexacli devices to see exact device names, then match them in your commands. Partial matching is supported.

Command not working

Try running the same command with alexacli command instead - this sends it as a voice command which has broader support.

How It Works

This CLI uses the same unofficial API that the Alexa mobile app uses. It:

  1. Exchanges your refresh token for session cookies
  2. Obtains a CSRF token from alexa.amazon.com
  3. Sends commands to pitangui.amazon.com (US) or layla.amazon.com (EU)

This approach is used by many popular projects including alexa-remote-control and Home Assistant's Alexa integration.

Disclaimer

This is an unofficial tool that uses Amazon's private APIs. It may break at any time if Amazon changes their API. Use at your own risk.

Acknowledgments

This project builds on the work of several excellent open source projects:

Without these projects' reverse-engineering efforts and documentation, this CLI wouldn't exist.

License

MIT

About

CLI for controlling Amazon Alexa devices

Resources

License

Stars

Watchers

Forks

Packages

No packages published