> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trdrs.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Put accounts into groups with tags

> Tag the accounts you issue and let your tag rules put each new one into the right group, so your backend never picks a group itself.

Tag the accounts you issue, and your tag rules put each new account into the group you chose for
its tag. A funding firm that runs a sign-up page with a partner, for example, tags those traders'
accounts `partner-north`. They land in their own group, with that group's route, pricing and rules,
and every other trader lands in your standard group.

Use this page when your backend issues accounts and the group should follow from who the trader is,
not from code that picks a group on every issue. By the end you have tag rules in force, an account
issued into a group by its tag, and the call that changes an account's tags later.

> [!WARNING]
>
> Tags and tag rules are deprecated, and the next release removes them: name the `groupId` on each
> issue instead. Tags no longer feed hedging: no hedging rule matches a tag. An account's introducing
> broker is its `introducingBroker`, never a tag.

## Before you start

Every call on this page uses the variables [Run a venue](https://docs.trdrs.co/guides/run-a-venue.md) sets:
`TRDRS_API_BASE_URL`, `TRDRS_VENUE_KEY` and `VENUE`, plus `ACCOUNT` for one account. In
TypeScript, the examples use this setup, and `accountId` for the account:

```ts theme={null}
const base = process.env.TRDRS_API_BASE_URL
const venue = process.env.TRDRS_VENUE_ID
const auth = { Authorization: `Bearer ${process.env.TRDRS_VENUE_KEY}` }
const json = { ...auth, 'content-type': 'application/json' }
```

Each group a rule names must already exist, and it needs a route before an account can be issued
into it. [Run a venue](https://docs.trdrs.co/guides/run-a-venue.md) creates groups with
`POST /api/venues/{venueId}/conditions/groups`. The examples use three: `standard`,
`partner-north` and `vip`.

An account tag is lowercase letters, digits and hyphens, starts with a letter or a digit, and is at
most 40 characters long. An account carries up to 20.

Every write needs an `Idempotency-Key` header of at most 128 characters. Send the same key and the
same body again and you get the first result back, so a retry after a timeout is safe.

## How a tag places an account

You issue an account with its tags and no `groupId`, trdrs reads your tag rules from the top, and
the account joins the group of the first rule whose tag it carries. An issue that names a `groupId`
joins that group, and its tags are kept on the account without reading the rules.

A tag places an account once, when it's issued. Changing your tag rules, or the tags on an account,
never moves an account you already issued. To move one, use
[Move an account you already issued](#move-an-account-you-already-issued).

## Set your tag rules

Read the rules in force first. You need their `revision` to change them.

Required scope: `venue:read`.

```bash cURL theme={null}
curl "$TRDRS_API_BASE_URL/api/venues/$VENUE/tag-rules" \
  -H "Authorization: Bearer $TRDRS_VENUE_KEY"
```

```ts TypeScript theme={null}
const res = await fetch(`${base}/api/venues/${venue}/tag-rules`, { headers: auth })
const { tagRules } = await res.json()
```

A venue that has never set rules gets `{"tagRules":{"rules":[],"revision":null}}`.

Now send the whole list, in the order you want it read, with the `revision` you read, or `null` the
first time. Each rule names one tag and the group a new account carrying it joins.

Required scope: `venue:configure`.

```bash cURL theme={null}
curl -X POST "$TRDRS_API_BASE_URL/api/venues/$VENUE/tag-rules" \
  -H "Authorization: Bearer $TRDRS_VENUE_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: tag-rules-1" \
  -d '{
    "rules": [
      { "tag": "partner-north", "groupId": "partner-north" },
      { "tag": "vip", "groupId": "vip" }
    ],
    "expectedRevision": null
  }'
```

```ts TypeScript theme={null}
const res = await fetch(`${base}/api/venues/${venue}/tag-rules`, {
  method: 'POST',
  headers: { ...json, 'Idempotency-Key': 'tag-rules-1' },
  body: JSON.stringify({
    rules: [
      { tag: 'partner-north', groupId: 'partner-north' },
      { tag: 'vip', groupId: 'vip' },
    ],
    expectedRevision: null,
  }),
})
const { tagRules } = await res.json()
```

The response is `200` with your `tagRules` and their new `revision`, here `1`. The first rule that
matches wins, so an account tagged both `partner-north` and `vip` joins `partner-north`. Put the rule
you want to win higher in the list.

Each change makes a new revision. A change sent with a stale `revision` is refused, so two people
editing the rules at once never overwrite each other. Sending the rules already in force changes
nothing and answers their current revision. A venue holds at most 100 rules.

| You get | It means |
| - | - |
| `400 invalid_tag` | A rule's tag isn't a valid tag. `field` names it, such as `rules[0].tag`. |
| `400 duplicate` | Two rules name the same tag. |
| `400 group_unknown` | A rule names a group your venue doesn't have, such as `rules[1].groupId`. |
| `400 too_many` | More than 100 rules. |
| `409 version_conflict` | The rules changed since the `revision` you sent. Read them again. |

## Issue an account by its tags

Issue the account as [Venue accounts](https://docs.trdrs.co/guides/venue-accounts.md) describes, but leave `groupId` out and
send its `tags`. Your tag rules decide its group.

Required scope: `account:issue`.

```bash cURL theme={null}
curl -X POST "$TRDRS_API_BASE_URL/api/venues/$VENUE/accounts" \
  -H "Authorization: Bearer $TRDRS_VENUE_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: signup-30517" \
  -d '{
    "email": "trader@example.com",
    "tags": ["partner-north"],
    "startingBalance": 50000,
    "currency": "USD",
    "riskPolicy": { "policyId": "meridian-futures" },
    "referenceId": "signup-30517"
  }'
```

```ts TypeScript theme={null}
const res = await fetch(`${base}/api/venues/${venue}/accounts`, {
  method: 'POST',
  headers: { ...json, 'Idempotency-Key': 'signup-30517' },
  body: JSON.stringify({
    email: 'trader@example.com',
    tags: ['partner-north'],
    startingBalance: 50000,
    currency: 'USD',
    riskPolicy: { policyId: 'meridian-futures' },
    referenceId: 'signup-30517',
  }),
})
const { account } = await res.json()
```

The response is `201` with the new `account`. Its `groupId` is `partner-north`, the group your first
matching rule names, and its `tags` are `["partner-north"]`. Leave out `startingBalance` or
`riskPolicy` when the group states them. A group that names its own risk policy issues only under it.

| You send | The account |
| - | - |
| A `groupId`, with or without `tags` | Joins the group you named, and keeps its tags |
| `tags` and no `groupId`, and a rule matches | Joins the group of the first rule whose tag it carries |
| `tags` and no `groupId`, and no rule matches | Isn't issued: `400 invalid_request` with `field` `groupId` |
| Neither `groupId` nor `tags` | Isn't issued: `400 invalid_request` with `field` `groupId` |

The same `referenceId` sent again answers the account it issued, with `created: false` and status
`200`, in the group it joined then, even if your rules changed since.

| You get | It means |
| - | - |
| `400 invalid_tag`, `duplicate` or `too_many` | A tag isn't valid, appears twice, or there are more than 20. `field` names it, such as `tags[0]`. |
| `409 group_has_no_route` | The group the rule names has no route yet. Point it at one, then issue again. |

## Change an account's tags

Set an account's tags when what you know about the trader changes. Send the whole list, with the
account's `tagsRevision` from `GET /api/venues/{venueId}/accounts`, or `null` for an account with no
tags yet.

Required scopes: `hedge:manage` and `hedge:execute`.

```bash cURL theme={null}
curl -X POST "$TRDRS_API_BASE_URL/api/venues/$VENUE/accounts/$ACCOUNT/tags" \
  -H "Authorization: Bearer $TRDRS_VENUE_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: tags-$ACCOUNT-2" \
  -d '{ "tags": ["partner-north", "vip"], "expectedRevision": 1 }'
```

```ts TypeScript theme={null}
const res = await fetch(`${base}/api/venues/${venue}/accounts/${encodeURIComponent(accountId)}/tags`, {
  method: 'POST',
  headers: { ...json, 'Idempotency-Key': `tags-${accountId}-2` },
  body: JSON.stringify({ tags: ['partner-north', 'vip'], expectedRevision: 1 }),
})
const { account } = await res.json()
```

The response is `200` with the account's `accountId`, its `tags` and their new `revision`. The account
stays in the group it's in. Its new tags are what your account list shows, and they place nothing and
move no hedge. Sending the tags it already carries changes nothing.

| You get | It means |
| - | - |
| `400 invalid_tag`, `duplicate` or `too_many` | A tag isn't valid, appears twice, or there are more than 20. |
| `404 not_found` | The account isn't one of your customer accounts. |
| `409 version_conflict` | The tags changed since the revision you sent. Read the account list again. |

## Move an account you already issued

Move an account into another group yourself when it should be priced or ruled differently from now
on. Send the group, or `null` to take it out of every group.

Required scope: `venue:configure`.

```bash cURL theme={null}
curl -X POST "$TRDRS_API_BASE_URL/api/venues/$VENUE/conditions/accounts/$ACCOUNT/group" \
  -H "Authorization: Bearer $TRDRS_VENUE_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: move-$ACCOUNT-vip" \
  -d '{ "groupId": "vip" }'
```

```ts TypeScript theme={null}
const res = await fetch(`${base}/api/venues/${venue}/conditions/accounts/${encodeURIComponent(accountId)}/group`, {
  method: 'POST',
  headers: { ...json, 'Idempotency-Key': `move-${accountId}-vip` },
  body: JSON.stringify({ groupId: 'vip' }),
})
const { assignment } = await res.json()
```

The response is the account's `assignment`: its `groupId` and `revision`. When the new group charges
differently, the move is refused as `409 not_quiescent` until the account is flat, so a trade never
changes pricing halfway.

## Test it end to end

Run it once in the sandbox with a test trader, never a real customer.

1. Create the groups `standard`, `partner-north` and `vip`, each with a route.
2. Set the two rules above and confirm `GET /api/venues/{venueId}/tag-rules` answers `revision` `1`.
3. Issue an account with `"tags": ["partner-north"]` and no `groupId`. Its `groupId` is
   `partner-north`.
4. Issue one with `"tags": ["newsletter"]` and no `groupId`. It's refused with `field` `groupId`,
   because no rule names that tag. Issue it again with `"groupId": "standard"`.
5. Swap the two rules and set them at revision `1`. Read `GET /api/venues/{venueId}/accounts`: the
   first account is still in `partner-north`.

## Next steps

> [card] [Venue accounts](https://docs.trdrs.co/guides/venue-accounts.md)
>
> Credit, pay out, reset and halt the accounts you issued.

> [card] [Hedge your venue's risk](https://docs.trdrs.co/guides/venue-hedging.md)
>
> Hedge each group's accounts by instrument, class, order size or results.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.