# Create help-desk requests from your app

Connect your client's own system — for example their service desk — to a project's help desk. It creates requests with files, reads their status and replies, with no person's account.

A help-desk token acts as one **connection**: one project, one client organisation and one read scope. Every call stays inside that connection.

## Before you start

- An Admin creates the connection in **Administration › Help desk**: the client organisation, the project, the abilities (`requests:create`, `requests:read`, `requests:reply`, `files:upload`) and the read scope — `own` (the requests that this connection made) or `organisation` (every request of the client in the project).
- The Admin issues the token. It starts with `kanso_hdt_` and shows one time. Keep it in a secret store.
- Help desk must be on for the project, and its request form must be on for a create.

*Terminal*

```bash
export API="https://app.youniton.example/api/v1/help-desk"
export AUTH="Authorization: Bearer $YOUNITON_HELPDESK_TOKEN"   # kanso_hdt_… — shown one time in Administration › Help desk
```

Send only the `Authorization` header: the token names the tenant, the connection and its project.

## The flow

1. **Read the form**

   The form of the connection's project gives the categories and the fields that a request needs.

   *GET /help-desk/form*

   ```bash
   curl -s -H "$AUTH" -H 'Accept: application/json' "$API/form"
   ```

   [`GET /help-desk/form`](https://docs.youniton.com/api/reference/help-desk.md#getIntakeForm) — needs `requests:read` or `requests:create`.

2. **Start an upload (optional)**

   To attach a file, start an upload intent. The target is `request_draft` for a new request, or `request` with a `requestKey` for a reply.

   *POST /help-desk/uploads*

   ```bash
   curl -s -X POST -H "$AUTH" -H 'Content-Type: application/json' -H "Idempotency-Key: $(uuidgen)" "$API/uploads" \
     --data '{"target":{"type":"request_draft"},"fileName":"receipt.pdf","sizeBytes":182044,"mimeType":"application/pdf"}'
   # → 201 data.fileId, data.put.url, data.put.headers
   ```

   [`POST /help-desk/uploads`](https://docs.youniton.com/api/reference/help-desk.md#startIntakeUpload) — needs `files:upload`.

3. **Send the file with one PUT**

   Send the bytes to the presigned URL with **one** `PUT`. Send each header of `data.put.headers` with it. The URL is valid for 15 minutes. Do not send your Youniton token to this URL.

   *PUT to the presigned URL*

   ```bash
   curl -s -X PUT -H 'Content-Type: application/pdf' --data-binary @receipt.pdf '<data.put.url>'
   ```

4. **Complete the upload**

   *POST /help-desk/uploads/{fileId}/complete*

   ```bash
   curl -s -X POST -H "$AUTH" -H "Idempotency-Key: $(uuidgen)" "$API/uploads/<fileId>/complete"
   ```

   Youniton starts the malware scan. [`POST /help-desk/uploads/{fileId}/complete`](https://docs.youniton.com/api/reference/help-desk.md#completeIntakeUpload)

   > **Upload limits**
   >
   > - One presigned `PUT`, no multipart. A file is at most the tenant's maximum and 100 MB (`422 file.too_large`). The type must be on the accept list (`422 file.type_not_accepted`).
   > - A connection holds at most 5 open intents (`409 file.too_many_open_intents`).
   > - An intent expires one hour after its start. A late completion answers `409 file.intent_expired`.

5. **Create the request**

   Send the category, the texts, your own `externalReference`, the completed `fileIds` and the person at the client who reported it.

   *POST /help-desk/requests*

   **curl**

   ```bash
   curl -s -X POST -H "$AUTH" -H 'Content-Type: application/json' \
     -H "Idempotency-Key: $(uuidgen)" "$API/requests" --data '{
     "categoryKey": "website_bug",
     "summary": "Gift cards cannot be applied",
     "description": "A gift card code is refused at checkout.",
     "impact": "urgent",
     "externalReference": "orbit-desk:case:88412",
     "fileIds": ["<fileId>"],
     "reporter": {"name": "Sam Lee", "email": "sam.lee@orbit-retail.example", "externalId": "orbit-desk-user-4471"}
   }'
   # → 201 {"data": {"key": "ORB-R22", "status": "in_review", …}}
   ```

   **JavaScript**

   ```js
   const res = await fetch("https://app.youniton.example/api/v1/help-desk/requests", {
     method: "POST",
     headers: {
       Authorization: `Bearer ${process.env.YOUNITON_HELPDESK_TOKEN}`,
       "Content-Type": "application/json",
       "Idempotency-Key": crypto.randomUUID(),
     },
     body: JSON.stringify({
       categoryKey: "website_bug",
       summary: "Gift cards cannot be applied",
       description: "A gift card code is refused at checkout.",
       impact: "urgent",
       externalReference: "orbit-desk:case:88412",
       fileIds: [fileId],
       reporter: { name: "Sam Lee", email: "sam.lee@orbit-retail.example", externalId: "orbit-desk-user-4471" },
     }),
   });
   const { data } = await res.json(); // res.status 201 (new) or 200 (the same reference and body)
   ```

   **Python**

   ```python
   import os, uuid, requests

   res = requests.post(
       "https://app.youniton.example/api/v1/help-desk/requests",
       headers={
           "Authorization": f"Bearer {os.environ['YOUNITON_HELPDESK_TOKEN']}",
           "Idempotency-Key": str(uuid.uuid4()),
       },
       json={
           "categoryKey": "website_bug",
           "summary": "Gift cards cannot be applied",
           "description": "A gift card code is refused at checkout.",
           "impact": "urgent",
           "externalReference": "orbit-desk:case:88412",
           "fileIds": [file_id],
           "reporter": {"name": "Sam Lee", "email": "sam.lee@orbit-retail.example", "externalId": "orbit-desk-user-4471"},
       },
   )
   request_key = res.json()["data"]["key"]
   ```

   **PHP**

   ```php
   <?php
   $res = $client->post('https://app.youniton.example/api/v1/help-desk/requests', [
       'headers' => [
           'Authorization' => 'Bearer ' . getenv('YOUNITON_HELPDESK_TOKEN'),
           'Idempotency-Key' => bin2hex(random_bytes(16)),
       ],
       'json' => [
           'categoryKey' => 'website_bug',
           'summary' => 'Gift cards cannot be applied',
           'description' => 'A gift card code is refused at checkout.',
           'impact' => 'urgent',
           'externalReference' => 'orbit-desk:case:88412',
           'fileIds' => [$fileId],
           'reporter' => ['name' => 'Sam Lee', 'email' => 'sam.lee@orbit-retail.example', 'externalId' => 'orbit-desk-user-4471'],
       ],
   ]);
   $requestKey = json_decode((string) $res->getBody(), true)['data']['key'];
   ```

   The same `externalReference` again gives the **same** request (`200`), never a second one. With another body it answers `409 help_desk_request.external_reference_exists` with the existing `requestKey`. [`POST /help-desk/requests`](https://docs.youniton.com/api/reference/help-desk.md#createIntakeRequest)

6. **Read it back, follow it and reply**

   *Reads and a reply*

   ```bash
   curl -s -H "$AUTH" "$API/requests/ORB-R22"
   curl -s -H "$AUTH" "$API/requests?status=all"            # meta.changeCursor → ?changeCursor=… for the next page or poll
   curl -s -H "$AUTH" "$API/requests/ORB-R22/messages"
   curl -s -X POST -H "$AUTH" -H 'Content-Type: application/json' -H "Idempotency-Key: $(uuidgen)" \
     "$API/requests/ORB-R22/messages" --data '{"body":"It also fails on iPad."}'
   ```

   - [`GET /help-desk/requests/{requestKey}`](https://docs.youniton.com/api/reference/help-desk.md#getIntakeRequest)One request as the connection reads it
   - [`GET /help-desk/requests`](https://docs.youniton.com/api/reference/help-desk.md#listIntakeRequests)The requests that the connection can read, oldest update first
   - [`GET /help-desk/requests/{requestKey}/messages`](https://docs.youniton.com/api/reference/help-desk.md#listIntakeRequestMessages)The client-visible messages, oldest first
   - [`POST /help-desk/requests/{requestKey}/messages`](https://docs.youniton.com/api/reference/help-desk.md#postIntakeRequestMessage)Reply on a request from the client's system
   - [`GET /help-desk/requests/{requestKey}/files/{fileId}/download`](https://docs.youniton.com/api/reference/help-desk.md#downloadIntakeRequestFile)Redirect to a 60-second signed URL of a file that the connection can read

   The list is a poll: read [the change cursor](https://docs.youniton.com/api/pagination.md#change-cursor). A file download answers `302` to a signed URL that is valid for 60 seconds.

7. **Withdraw it (optional)**

   *POST /help-desk/requests/{requestKey}/withdraw*

   ```bash
   curl -s -X POST -H "$AUTH" -H "Idempotency-Key: $(uuidgen)" "$API/requests/ORB-R22/withdraw"
   ```

   Only the connection that made the request can withdraw it, and only while it is open (`403 access.forbidden` with `reason: not_requester`). [`POST /help-desk/requests/{requestKey}/withdraw`](https://docs.youniton.com/api/reference/help-desk.md#withdrawIntakeRequest)

> **Idempotency-Key**
>
> Send an `Idempotency-Key` on each creating `POST`, as on every creating call of the API. The reference lists the operations that need it.

## What each refusal means

| Answer | Meaning |
|---|---|
| `401 auth.unauthenticated` | the token is wrong, expired or revoked, or the connection is revoked |
| `403 help_desk.ability_missing` | the connection does not have the ability of the operation |
| `403 help_desk.connection_paused` | an Admin paused the connection |
| `403 help_desk.unavailable` | help desk or its form is off for the project |
| `404 help_desk_request.not_found` | the request is not of the connection's project and client, or not in its read scope |
| `409 help_desk_request.external_reference_exists` | the external reference names a request with another body |
| `429 rate_limit.exceeded` | too many calls, creates or uploads; wait for `Retry-After` |

## What the team sees

Each request shows in the project's Help desk queue as *API connection · \<connection name>*. When the reporter's e-mail is the e-mail of a portal guest of the same client, the request shows in that guest's portal list as theirs. Youniton never e-mails a reporter: your system learns of changes by polling or by webhooks.

## Get changes by webhook

A connection can subscribe its client's system to request events: `request.created`, `request.updated`, `request.message_posted`, `request.decided` and `request.closed`. A connection never gets an event for its own action. The payload holds the request as the connection may read it, without the description and the field values: read them with [`GET /help-desk/requests/{requestKey}`](https://docs.youniton.com/api/reference/help-desk.md#getIntakeRequest). Read [Receive Youniton webhooks](https://docs.youniton.com/api/guides/receive-webhooks.md).
