Integrations

PowerShell → AI24X

Handy on Windows servers and ops scripts.

Invoke-RestMethod

$key = $env:AI24X_API_KEY $body = @{ prompt = "Hello"; model = "flash"; max_tokens = 200 } | ConvertTo-Json $r = Invoke-RestMethod -Method Post -Uri "https://api.ai24x.com/v1/chat/run" ` -Headers @{ "X-API-Key" = $key; "Content-Type" = "application/json" } ` -Body $body $r | ConvertTo-Json -Depth 6

Do not commit keys into scripts. For VIP named models, set model to vip-kimi etc. after VIP is active.

Prerequisites

  • Windows PowerShell 5.1 (Windows Server 2016 and later) or PowerShell 7+. Invoke-RestMethod ships with both, so there is nothing to install.
  • An AI24X API key, created from the console after you sign up. Keep it in an environment variable rather than inside the script.
  • Outbound HTTPS access to api.ai24x.com on port 443.

Set the key once per session so it never lands in your script file or your shell history:

$env:AI24X_API_KEY = Read-Host "AI24X API key"

Full example with error handling

A bare call is fine as a smoke test. In a scheduled task you want failures to be visible, so wrap the request:

$body = @{ prompt = "Summarise this log"; model = "flash"; max_tokens = 300 } | ConvertTo-Json try { $r = Invoke-RestMethod -Method Post -Uri "https://api.ai24x.com/v1/chat/run" ` -Headers @{ "X-API-Key" = $env:AI24X_API_KEY; "Content-Type" = "application/json" } ` -Body $body -TimeoutSec 60 $r | ConvertTo-Json -Depth 6 } catch { Write-Error "AI24X call failed: $($_.Exception.Message)" if ($_.Exception.Response) { Write-Host "HTTP status:" $_.Exception.Response.StatusCode.value__ } }

Choosing a tier

Every request carries a model value. Use flash for high-volume, low-stakes work such as classification, extraction and log summaries. Move a call to pro only when it genuinely needs more reasoning or a longer context. Named VIP models use a vip- prefix, for example vip-kimi, and only resolve once VIP access is active on the account.

Starting every script on flash and stepping the individual calls up is the single biggest lever on a monthly bill.

Common errors and fixes

  • 401 Unauthorized — the key is missing, revoked, or carries a trailing newline. Re-run the Read-Host line above and check for invisible whitespace.
  • 402 / insufficient credits — top up the AI24X credit balance, then send the identical request again.
  • 400 Bad Request — the body did not parse. Build it with @{ ... } | ConvertTo-Json instead of a hand-written string, and raise -Depth when you post nested objects.
  • 429 Too Many Requests — requests arrived faster than your plan allows. Add a short pause inside loops rather than retrying immediately.
  • HTML back instead of JSON — something between you and the API answered, usually a corporate proxy. Confirm the URI starts with https://api.ai24x.com/v1/.

One PowerShell quirk worth knowing: Invoke-RestMethod throws on any non-2xx status, so a failed call jumps straight to catch and $r stays empty. If you would rather read the body yourself, use Invoke-WebRequest, or add -SkipHttpErrorCheck on PowerShell 7+.

Troubleshooting order

  1. Reproduce the call with curl. If curl succeeds and the script does not, the fault is local, not the key.
  2. Print the HTTP status before parsing anything.
  3. Verify the key: correct account, still active, no whitespace.
  4. Verify the body is valid JSON by pasting the ConvertTo-Json output.
  5. Only then investigate timeouts, proxy settings and TLS on the host.

curl · OpenClaw · Console