# Create a link

Create a payment link

`POST https://api.cadmospay.com/machine/v1/gateway/links`

The link belongs to your business wallet. Send the `checkoutUrl` to your customer.

Always send a `requestId`: it becomes the link's `uuid`, so retrying a create that timed out returns the same link instead of a second one. Limits: 30 creates a minute and 500 a day.

## Authentication

`Authorization: Bearer <accessToken>`

The `accessToken` from `POST /v1/external-auth/token`, valid 15 minutes.

## Example requests

```bash
curl --fail-with-body 'https://api.cadmospay.com/machine/v1/gateway/links' \
  -H "Authorization: Bearer ${CADMOS_ACCESS_TOKEN:?Set CADMOS_ACCESS_TOKEN}" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "description": "Order #42",
  "externalReference": "order-42",
  "amountUsd": "100.00",
  "requestId": "${REQUEST_ID:?Set REQUEST_ID}"
}
JSON
```

```javascript
const { CADMOS_ACCESS_TOKEN, REQUEST_ID } = process.env;
if (!CADMOS_ACCESS_TOKEN || !REQUEST_ID) {
  throw new Error("Set CADMOS_ACCESS_TOKEN and REQUEST_ID");
}

const url = new URL("https://api.cadmospay.com/machine/v1/gateway/links");
const body = {
  "description": "Order #42",
  "externalReference": "order-42",
  "amountUsd": "100.00",
  "requestId": REQUEST_ID
};
const response = await fetch(url, {
  method: "POST",
  redirect: "error",
  headers: {
    Authorization: "Bearer " + CADMOS_ACCESS_TOKEN,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
});
console.log(response.status, await response.json());
```

```python
from http.client import HTTPSConnection
import json
import os

conn = HTTPSConnection("api.cadmospay.com", timeout=30)
path = "/machine/v1/gateway/links"
body = json.dumps({
  "description": "Order #42",
  "externalReference": "order-42",
  "amountUsd": "100.00",
  "requestId": os.environ["REQUEST_ID"]
})
headers = {
    "Authorization": "Bearer " + os.environ["CADMOS_ACCESS_TOKEN"],
    "Content-Type": "application/json",
}
conn.request("POST", path, body=body, headers=headers)
response = conn.getresponse()
print(response.status, response.read().decode())
conn.close()
```

## Request body

`application/json`

[CreateLinkRequest](https://cadmospay.com/developers/schemas/create-link-request.md)

Only the listed properties are accepted.

- `description` (string, required, length 1 to 200). Shown to the payer. Trimmed.
- `externalReference` (string, required, length 1 to 120). Your own reference, such as an order number. Returned with every settlement. Trimmed.
- `requestId` (string, optional, pattern ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$). A UUID v4 you choose; it becomes the link's `uuid`. Reuse it to retry the same create.
- `localMethods` (array of string, optional, items at most 5, no duplicates, each: pattern ^[a-z0-9_-]{2,40}$). Cash methods the link should also accept, such as `whish`. The ones your account may not offer are left out and listed in `refusedLocalMethods`. Absent: crypto only.
- `amountUsd` (string, optional, pattern ^\d{1,7}(\.\d{1,2})?$). The price in dollars, from 0.50 to 1000000, as a decimal string (`"100"`, `"99.5"`), never a JSON number. The Gateway Fee is yours: it comes out of what is counted and the payer never sees it. Absent: the payer chooses what to send.

```json
{
  "description": "Order #42",
  "externalReference": "order-42",
  "requestId": "0b6f5a8e-1c2d-4e3f-9a8b-7c6d5e4f3a2b",
  "amountUsd": "100.00",
  "localMethods": [
    "whish"
  ]
}
```

```json
{
  "description": "Donation",
  "externalReference": "donation-7",
  "requestId": "5d4c3b2a-1f0e-4d9c-8b7a-6f5e4d3c2b1a"
}
```

## Responses

### 201

The link, ready to pay.

[CreatedLink](https://cadmospay.com/developers/schemas/created-link.md)

```json
{
  "uuid": "0b6f5a8e-1c2d-4e3f-9a8b-7c6d5e4f3a2b",
  "status": "active",
  "checkoutUrl": "https://cadmospay.com/checkout/example_checkout_token_for_documentation_00",
  "receiverAddress": "0x0000000000000000000000000000000000000042",
  "externalReference": "order-42",
  "localMethods": [],
  "paymentReference": null,
  "amountUsd": "100.00",
  "countedUsd": "0.00",
  "leftUsd": "100.00",
  "paid": false,
  "token": "example_checkout_token_for_documentation_00",
  "refusedLocalMethods": []
}
```

### 401

No token, or a token that is expired or not for this route: sign in again.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 403

Your identity is not allowed this route, or has no business wallet.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 404

`GATEWAY_LINK_NOT_FOUND`: another business already used this `requestId`. Send a new one.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 409

`GATEWAY_REQUEST_CONFLICT`: this `requestId` was used with another description, reference or amount. `GATEWAY_LINK_VOID`: its link was voided. Send a new `requestId` for a new link.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 422

The request does not match this contract: `VALIDATION_ERROR` lists each field and the rule it broke. An amount outside $0.50 to $1,000,000 is refused here too.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 429

Over a limit. Wait and retry.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 5XX

A Cadmos service failed or did not answer (`{ "message": "Service Error" }`). Retry; retry a create with the same `requestId`.

[Error](https://cadmospay.com/developers/schemas/error.md)
