List stopped machines (or with a certain status generally) via API

Hello team,

I know that you can list machines via the CLI and can perform some shell based filtering to obtain those with a certain status (in my case stopped).

My question is whether there’s also API support for something like this planned? The docs don’t show this option.

For context: I’m using machines in a “serverless” way and end up with dangling stopped machines sometimes. I’d like to have a scheduled job clean them up via the API rather than having to manually type a CLI command.

Thanks!

The list machines endpoint returns all under your application.
You can filter by machines with the state of "stopped" :slight_smile:

I’ve done this recently using these calls (these are authenticates requests):

  • Call to GET https://api.machines.dev/v1/apps?org_slug=<my-org>
    • Foreach app in the results, check app.machine_count, if greater than zero, then do further work.
      • Foreach app with a machine, run GET https://api.machines.dev/v1/apps/<app-name>/machines to get the machines and their state
MY_ORG="personal"
curl -H "Authorization: Bearer `fly auth token`" \
    https://api.machines.dev/v1/apps?org_slug=$MY_ORG

# Logic here to get JSON results and get each app with some machines

APP_NAME="some_app_from_results_above"
curl -H "Authorization: Bearer `fly auth token`" \
    https://api.machines.dev/v1/apps/$APP_NAME/machines

Thanks for the boilerplate, helps a lot!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.