# Code samples

> Decision API code samples in Python, JavaScript and curl, ready to copy.

Section: Resources. Web page: https://www.launchables.ai/docs/samples

## Python

```
import os, requests

def decide(state, questions, endpoint="https://api.launchables.ai"):
    response = requests.post(
        f"{endpoint}/v1/decide",
        headers={"Authorization": f"Bearer {os.environ['LAUNCHABLES_API_TOKEN']}"},
        json={"state": state, "questions": questions},
        timeout=30,
    )
    response.raise_for_status()
    return response.json()["answers"]

answers = decide(
    "Customer bought the item 12 days ago and has no receipt.",
    {"refund": {"type": "noul", "instructions": "Is a refund permitted?"}},
)
if answers["refund"]["noul"] >= 0.9:
    print("approve")
elif answers["refund"]["noul"] <= 0.1:
    print("decline")
else:
    print("send to a person")
```

## JavaScript

```
const response = await fetch('https://api.launchables.ai/v1/decide', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.LAUNCHABLES_API_TOKEN}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    state: { from: 'J. Smith', message: 'Is my parcel lost? It is a week late.' },
    questions: {
      priority: { type: 'score', instructions: 'How urgent is this?', criteria: ['Routine', 'This week', 'Today'] },
    },
  }),
});
if (!response.ok) throw new Error((await response.json()).error);
const { answers } = await response.json();
console.log(answers.priority.level, answers.priority.probabilities);
```

Call the API from your server, not from a web page: the token must stay secret.

On Node, the [JavaScript SDK](https://www.launchables.ai/docs/sdk-javascript) does this for you, with typed answers, retries and error classes.

## Retrying with backoff

```
import time, requests

def decide_with_retry(payload, headers, attempts=4):
    for attempt in range(attempts):
        response = requests.post("https://api.launchables.ai/v1/decide", json=payload, headers=headers, timeout=30)
        if response.status_code not in (429, 502, 503):
            response.raise_for_status()
            return response.json()
        time.sleep(2 ** attempt)
    response.raise_for_status()
```

## curl: several questions

```bash
curl https://api.launchables.ai/v1/decide \
  -H "Authorization: Bearer $LAUNCHABLES_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "state": {"from": "J. Smith", "message": "Charged twice for order 4471, need the extra payment back"},
    "questions": {
      "route":    {"type": "choice", "instructions": "Which team?", "criteria": ["billing", "returns", "technical", "other"]},
      "refund":   {"type": "noul",   "instructions": "Is the customer asking for money back?"},
      "priority": {"type": "score",  "instructions": "How urgent?", "criteria": ["Routine", "This week", "Today"]}
    }
  }'
```
