# Create relation

Add a company group link or a third-party payer to a counterparty.

Adds a relation between the counterparty in the path and another one. direction is read from the counterparty in the path: parent and subsidiary build a company group, paid_by and pays_for declare a third-party payer. Matching then accepts a payment from the related counterparty like one from the customer, for the invoices dated inside the relation dates. The relation is stored with source api. Unknown fields are rejected.


- HTTP method: `POST`
- Path: `/api/v1/counterparties/:code/relations`
- URL: `https://api.invunion.com/api/v1/counterparties/:code/relations`
- Required scope: `counterparties:write`
- Idempotency-Key header: accepted. See https://www.invunion.com/knowledge-base/api/idempotency/
- HTML docs: https://www.invunion.com/knowledge-base/api/create-counterparty-relation/
- Markdown docs: https://www.invunion.com/knowledge-base/api/create-counterparty-relation.md

## Path parameters

| Name | Type | Required | Description | Allowed values | Example |
| --- | --- | --- | --- | --- | --- |
| `code` | string, max 50 | required | Counterparty code (`account_code`). |  | `CPT-042` |


## Body parameters

| Name | Type | Required | Description | Allowed values | Example |
| --- | --- | --- | --- | --- | --- |
| `direction` | string, no maximum | required | What the other counterparty is to the one in the path. parent: its parent company. subsidiary: one of its subsidiaries. paid_by: it pays the invoices of the one in the path. pays_for: the one in the path pays its invoices. | `parent`, `subsidiary`, `paid_by`, `pays_for` |  |
| `relatedCounterpartyId` | string, max 255 | required | Id or account code of the other counterparty. |  | `CPT-007` |
| `sharePercent` | number | optional | Third-party payers only. Share of each invoice the payer covers, as a percentage of the total including VAT, above 0 and at most 100. null or omitted means the full amount. |  | `70` |
| `trust` | string, no maximum | optional | customer: a payment from the related counterparty counts like one from the customer. review: its matches always wait in the Match inbox. Default customer. | `customer`, `review` |  |
| `validFrom` | string, no maximum | optional | First invoice date the relation applies to, as YYYY-MM-DD. null or omitted means no start. |  | `2026-01-01` |
| `validTo` | string, no maximum | optional | Last invoice date the relation applies to, as YYYY-MM-DD. null or omitted means no end. |  | `2026-12-31` |
| `notes` | string, max 1000 | optional | Free text for your team. |  | `Insurer covers 70% under contract 2026-A.` |

## Errors

| Error | HTTP code | Description |
| --- | --- | --- |
| `Missing Bearer token` | `401` | The Authorization header is missing or is not a Bearer token. |
| `Invalid or revoked API key` | `401` | The API key is unknown, malformed, expired, or has been revoked. |
| `API key is missing scope counterparties:write` | `403` | The key does not include counterparties:write. A write scope does not imply the matching read scope. |
| `Too many requests, please try again later` | `429` | More than 100 requests in one minute from this API key or IP address. Wait for the Retry-After header (seconds) and retry. |
| `sharePercent: Too big: expected number to be <=100` | `400` | A field of the body is missing, unknown or out of range. The message names the field. |
| `A share only applies to a third-party payer` | `400` | sharePercent was sent for a company group relation. |
| `validTo must be on or after validFrom` | `400` | The dates of the relation are in the wrong order. |
| `A counterparty cannot be related to itself` | `400` | relatedCounterpartyId is the counterparty in the path. |
| `A system counterparty cannot have relations` | `400` | One of the two counterparties is a system record, such as Unclassified. |
| `Counterparty not found` | `404` | No counterparty with this code exists in the authenticated tenant. |
| `The payers' shares would exceed 100% (110.00%)` | `409` | Together, the payers of one counterparty cannot cover more than 100% of its invoices for the same dates. |
| `This counterparty already has a parent company for these dates` | `409` | A counterparty has one parent company at a time. |
| `This relation would make a counterparty its own parent company` | `409` | A company group cannot loop. |
| `This payer is already set for these dates` | `409` | The same payer is entered once per period for one counterparty. |
| `Internal server error` | `500` | Unexpected server error. The JSON body includes correlationId. Retry with backoff. |
| `Idempotency-Key must be 8-255 characters: letters, digits, ".", "_", ":" or "-".` | `400` | The Idempotency-Key header is present but malformed. |
| `A request with this Idempotency-Key is still being processed.` | `409` | The first request with this key has not finished. Retry after it completes. code is idempotency_in_progress. |
| `This Idempotency-Key was already used with a different request body.` | `422` | Send a new key for a different request. code is idempotency_key_reuse. |

## Request (curl)

```bash
curl --request POST \
  --url https://api.invunion.com/api/v1/counterparties/CPT-042/relations \
  --header 'accept: application/json' \
  --header 'authorization: Bearer uk_live_YOUR_API_KEY' \
  --header 'content-type: application/json' \
  --data '{
  "direction": "paid_by",
  "relatedCounterpartyId": "CPT-007",
  "sharePercent": 70,
  "trust": "customer",
  "validFrom": "2026-01-01"
}'
```

## Request (Python)

```python
import requests

url = "https://api.invunion.com/api/v1/counterparties/CPT-042/relations"
headers = {
    "Accept": "application/json",
    "Authorization": "Bearer uk_live_YOUR_API_KEY",
    "Content-Type": "application/json",
}
payload = {
    "direction": "paid_by",
    "relatedCounterpartyId": "CPT-007",
    "sharePercent": 70,
    "trust": "customer",
    "validFrom": "2026-01-01"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
```

## Request (Ruby)

```ruby
require 'net/http'
require 'json'
require 'uri'

uri = URI("https://api.invunion.com/api/v1/counterparties/CPT-042/relations")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['Accept'] = 'application/json'
request['Authorization'] = 'Bearer uk_live_YOUR_API_KEY'
request['Content-Type'] = 'application/json'
request.body = "{\n  \"direction\": \"paid_by\",\n  \"relatedCounterpartyId\": \"CPT-007\",\n  \"sharePercent\": 70,\n  \"trust\": \"customer\",\n  \"validFrom\": \"2026-01-01\"\n}"
response = http.request(request)
puts response.body
```

## Request (JavaScript)

```javascript
const response = await fetch("https://api.invunion.com/api/v1/counterparties/CPT-042/relations", {
  method: "POST",
  headers: {
    "Accept": "application/json",
    "Authorization": "Bearer uk_live_YOUR_API_KEY",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
  "direction": "paid_by",
  "relatedCounterpartyId": "CPT-007",
  "sharePercent": 70,
  "trust": "customer",
  "validFrom": "2026-01-01"
}),
});
const data = await response.json();
```

## Request (Go)

```go
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
)

func main() {
	payload := []byte(`{
  "direction": "paid_by",
  "relatedCounterpartyId": "CPT-007",
  "sharePercent": 70,
  "trust": "customer",
  "validFrom": "2026-01-01"
}`)
	req, err := http.NewRequest("POST", "https://api.invunion.com/api/v1/counterparties/CPT-042/relations", bytes.NewBuffer(payload))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Authorization", "Bearer uk_live_YOUR_API_KEY")
	req.Header.Set("Content-Type", "application/json")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
}
```

## Request (Node)

```javascript
const response = await fetch("https://api.invunion.com/api/v1/counterparties/CPT-042/relations", {
  method: "POST",
  headers: {
    "Accept": "application/json",
    "Authorization": "Bearer uk_live_YOUR_API_KEY",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
  "direction": "paid_by",
  "relatedCounterpartyId": "CPT-007",
  "sharePercent": 70,
  "trust": "customer",
  "validFrom": "2026-01-01"
}),
});
console.log(await response.json());
```

## Success (201)

```json
{
  "success": true,
  "data": {
    "id": "3f2a9c1e-7b4d-4e8a-9c21-5d6e7f8a9b0c",
    "kind": "pays_for",
    "counterpartyId": "b7c1f0a2-5d3e-4f6a-8b9c-0d1e2f3a4b5c",
    "relatedCounterpartyId": "d4e5f6a7-8b9c-4d0e-9f1a-2b3c4d5e6f7a",
    "sharePercent": 70,
    "trust": "customer",
    "validFrom": "2026-01-01",
    "validTo": null,
    "source": "api",
    "notes": null
  }
}
```
