# Answer feedback

`POST /v1/generation/feedback` records your feedback on one answer: what went wrong, in one of
seven categories, with a comment if you want one.

## Request

Send it with a management key, read from `OPENDUNES_MANAGEMENT_KEY`. An API key gets a 401. The
[API keys](/docs/api-keys) guide shows how the two kinds of key differ. The feedback is recorded
against the request its handle names; [Receipt](/docs/reference/generation) reads the same
request back.

A customer asks Souk Bot the price of a kaftan, and the answer quotes last season's. Yacine
takes the handle from that answer's `X-Generation-Id` header and reports it as
`incorrect_response`.

| Field | Needed | You send |
|---|---|---|
| `generation_id` | yes | the receipt handle the answer carried in its `X-Generation-Id` header |
| `category` | yes | `latency`, `incoherence`, `incorrect_response`, `formatting`, `billing`, `api_error` or `other` |
| `comment` | no | what happened, in your words, up to 1 000 characters |

You send feedback only on your own account's requests: any other handle is a 404. The handle in
each sample is an example of the shape; put the one your answer carried in its place.

```python
import json
import os
import urllib.request

MANAGEMENT_KEY = os.environ["OPENDUNES_MANAGEMENT_KEY"]
ADDRESS = "https://opendunes.com/v1/generation/feedback"
GENERATION_ID = "gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG"

feedback = {
    "generation_id": GENERATION_ID,
    "category": "incorrect_response",
    "comment": "Quoted last season's price for the kaftan.",
}

request = urllib.request.Request(
    ADDRESS,
    data=json.dumps(feedback).encode(),
    headers={
        "Authorization": "Bearer " + MANAGEMENT_KEY,
        "Content-Type": "application/json",
    },
)
with urllib.request.urlopen(request) as response:
    print(json.load(response)["data"]["success"])
```

```javascript
const MANAGEMENT_KEY = process.env.OPENDUNES_MANAGEMENT_KEY;
const ADDRESS = "https://opendunes.com/v1/generation/feedback";
const GENERATION_ID = "gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG";

const response = await fetch(ADDRESS, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${MANAGEMENT_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    generation_id: GENERATION_ID,
    category: "incorrect_response",
    comment: "Quoted last season's price for the kaftan.",
  }),
});
const { data } = await response.json();

console.log(data.success);
```

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

const (
	address      = "https://opendunes.com/v1/generation/feedback"
	generationID = "gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG"
)

func main() {
	managementKey := os.Getenv("OPENDUNES_MANAGEMENT_KEY")

	body, err := json.Marshal(map[string]string{
		"generation_id": generationID,
		"category":      "incorrect_response",
		"comment":       "Quoted last season's price for the kaftan.",
	})
	if err != nil {
		panic(err)
	}

	req, err := http.NewRequest("POST", address, bytes.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Authorization", "Bearer "+managementKey)
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	var answer struct {
		Data struct {
			Success bool `json:"success"`
		} `json:"data"`
	}
	if err := json.NewDecoder(res.Body).Decode(&answer); err != nil {
		panic(err)
	}
	fmt.Println(answer.Data.Success)
}
```

```bash cURL
ADDRESS="https://opendunes.com/v1/generation/feedback"
GENERATION_ID="gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG"

curl "$ADDRESS" \
  -H "Authorization: Bearer $OPENDUNES_MANAGEMENT_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "generation_id": "'"$GENERATION_ID"'",
    "category": "incorrect_response",
    "comment": "Quoted last season'"'"'s price for the kaftan."
  }'
```

## Response

`200 OK`.

| Field | Holds |
|---|---|
| `data` | the outcome |
| `success` | in `data`: `true` when the feedback was recorded |

## What it cost

Free. Sending feedback is never charged.

## Errors

`error.code` carries the status, and `error.message` says why.

| Status | When |
|---|---|
| 400 | `generation_id` or `category` is missing, `category` is not one of the seven, or `comment` passes 1 000 characters |
| 401 | the key is missing, wrong, revoked or expired, or it is an API key |
| 404 | no request of your account carries this handle |
