# Send requests from code

Ask for approval from TypeScript, Python or plain HTTP, wait for the decision, and run only the approved arguments.

Get started. About 5 minutes. HTML: https://promptcompute.ai/docs/send-requests

## Before you start

- An API key from API keys (a test key is free and only reaches the test environment)
- Node 20 or later, or Python 3.9 or later

## Steps

1. **Install the SDK** The TypeScript SDK is on npm: `npm i @promptcompute/sdk`. The `promptcompute` Python package is not on PyPI yet; until it is, use the Plain HTTP step below or the MCP server from Python. The Python example below shows the package as it will be used.

2. **Set your key** Store the key as a secret. The SDKs read `PROMPTCOMPUTE_API_KEY`.

```bash
export PROMPTCOMPUTE_API_KEY=pc_test_...
```

3. **Ask and wait** `requestAndWait` sends the request and waits for a decision. It throws on reject, deny or expiry, so the action never runs without approval.

```ts
import { PromptCompute } from "@promptcompute/sdk";

const pc = new PromptCompute();
const decision = await pc.requestAndWait({
  action: "payment.refund",
  args: { order_id: "A-1001", amount: 120 },
  reason: "Customer reported damage",
});
await refund(decision.final_args); // never the original args
```

4. **Or wrap a function** `withApproval` returns a function that asks first and runs with the approved arguments. In Python, use the `@requires_approval` decorator.

```python
from promptcompute import requires_approval

@requires_approval(action="payment.refund")
def refund(order_id: str, amount: float) -> None:
    ...
```

5. **Plain HTTP** POST the request, then long-poll its wait endpoint for up to 50 seconds at a time.

```bash
curl -X POST https://api.promptcompute.ai/v1/approvals \
  -H "Authorization: Bearer $PROMPTCOMPUTE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"action":"payment.refund","args":{"order_id":"A-1001","amount":120}}'

curl "https://api.promptcompute.ai/v1/approvals/apr_.../wait?timeout=50" -H "Authorization: Bearer $PROMPTCOMPUTE_API_KEY"
```

## Use cases

### A batch job that changes billing

- Situation: A nightly job adjusts customer plans and you want a person on anything above a threshold.
- What to do: Wrap the change in `withApproval` with an idempotency key from the run id.
- Result: Retries never ask twice, and changes run only with the approved values.

### An agent framework

- Situation: Your agent runs on LangGraph, the OpenAI Agents SDK, Temporal or Inngest.
- What to do: Use the adapter from `@promptcompute/sdk` for your framework instead of writing the wait loop.
- Result: The run pauses at the step that needs approval and resumes with the decision.

## What you will see

Your code waits at the request, and continues with `final_args` once someone approves.

- The request shows in the inbox with your reason
- Rejected requests raise an error and the action does not run
- After approval, `final_args` holds any edits the approver made

requestAndWait after an approver lowered the amount:

```json
{
  "id": "apr_01j...",
  "status": "approved_with_edits",
  "action": "payment.refund",
  "args": { "order_id": "A-1001", "amount": 120 },
  "final_args": { "order_id": "A-1001", "amount": 80 },
  "risk": "medium",
  "rule_id": "larger-refunds"
}
```

## If something goes wrong

- **401 unauthorized.** The key is missing, malformed or revoked. Keys start with `pc_live_` or `pc_test_`; create a new one under API keys.
- **402 quota_exceeded.** The organization has no plan, its free plan has ended, or an org still in the free plan's notice period is past 120% of its 500 monthly requests. Buy a PromptReports family plan under Billing, or use the test environment, which is never limited.
- **Unknown actions always need approval at high risk.** Register the action under Action types, or import it from the starter catalog, so it gets a summary and the right risk.

## Related

- [Write approval policies](https://promptcompute.ai/docs/policies.md)
- [Get decisions by webhook](https://promptcompute.ai/docs/webhooks.md)
- [Connect Claude Code and other MCP clients](https://promptcompute.ai/docs/connect-claude-code.md)
