Invoke-RestMethod
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-RestMethodships 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.comon port 443.
Set the key once per session so it never lands in your script file or your shell history:
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:
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-Hostline 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-Jsoninstead of a hand-written string, and raise-Depthwhen 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
- Reproduce the call with curl. If curl succeeds and the script does not, the fault is local, not the key.
- Print the HTTP status before parsing anything.
- Verify the key: correct account, still active, no whitespace.
- Verify the body is valid JSON by pasting the
ConvertTo-Jsonoutput. - Only then investigate timeouts, proxy settings and TLS on the host.
Gateway guides: What is an AI gateway · OpenAI-compatible API · Pricing · Start free