<!-- Generated by scripts/build-docs.js. Edit docs/swagger.yaml or docs/guides/, then run `npm run docs:build`. -->

# Set payroll amounts

`POST https://marketplace-api.sequifi.com/v1/payroll`

Module: Payroll · operationId: `upsertPayroll` · Auth: `Authorization: Bearer <token>`
Web page: https://apidocs.sequifi.com/#/api/upsert-payroll

Writes commission, override and salary amounts onto each user's open pay stub for one pay period, creating the stub if the user does not have one yet. Up to 10 users per request. Only for companies on SequiPay payroll. It stores the amounts you send; it does not calculate pay, taxes or net pay, and it does not run, finalize or pay the payroll.

**Before you call:**

- Your company must run payroll in SequiPay. Otherwise every call returns `400 Sequipay payroll is not enabled for this company`.
- Get the exact pay frequency name from GET /v1/pay-frequency (for example `Weekly`). Ids are not accepted.
- Get user ids and each user's worker type (`1099` or `w2`) from GET /v1/users. One request covers one worker type, and every user in it must have that worker type.
- Each user needs a position on or before `pay_period_to` (set by onboarding or a position change). Users without one are skipped.
- Custom field names must match your company's payroll custom fields exactly: GET /v1/custom-fields?type=payroll.

**What happens:**

- The whole request is checked first. A problem here rejects everything with `400`: missing or badly formatted fields, more than 10 users, a repeated `user_id`, an unknown pay frequency, a worker type your company does not use, a period that is already finalized, or a period that starts on or before the end of the last finalized period for that worker type and pay frequency.
- Then each user is handled on their own, in their own transaction. A user who fails is listed in `data.results[]` with `status: false` and a message; the others still go through.
- If the user already has a stub for exactly this period, pay frequency and worker type, its `commission`, `override` and salary (`hourly_salary`) are replaced with the values you send.
- If not, a new open stub is created with your amounts, the user's position as of `pay_period_to`, and every other amount (net pay, taxes, deductions, reimbursements, adjustments) at `0`.
- Custom fields you send are created or replaced on the stub. Custom fields you leave out are not changed.
- Nothing checks that the period matches your company's pay calendar; you choose the dates.

**Idempotency and retries:**

- Amounts are absolute, not added: sending the same request twice leaves the same values, so a retry never doubles pay.
- A retry after a timeout is safe. Users that already went through are updated with the same values.

**Formats:**

- Dates are `YYYY-MM-DD`, and `pay_period_to` must be after `pay_period_from`. Send real calendar dates: the format check accepts an impossible date such as `2026-02-30`, which then fails for that user or misbehaves.
- Amounts are dollars. `commission_amount` and `override_amount` are required and must be 0 or more. `salary` is optional; a missing, negative or non-numeric `salary` is stored as `0` without an error.
- `worker_type` is case-insensitive (`W2` works). `user_id` may be a number or a string of digits.

**Errors:**

- `400` for the request-level problems above, with details in `data` (see the examples). The "after the last finalized payroll period" message prints that date in a long format, for example `Sat Feb 14 2026 00:00:00 GMT+0000 (Coordinated Universal Time)`.
- `200` with `status: false` per user for: user not found, a different worker type, no position on or before `pay_period_to`, a missing or negative amount, an unknown custom field name, or a database error ("Failed to process payroll."). A `200` can mean every user failed: check every result.
- `401` `{"message":"Unauthorized"}` for a missing or invalid token.

**Common mistakes:**

- Sending a pay frequency id (`2`) instead of its name (`Weekly`).
- Mixing 1099 and W-2 users in one request. Send one request per worker type.
- Expecting net pay: this endpoint writes gross commission, override and salary amounts only.

**Related:**

- GET /v1/payroll lists open stubs for a period, so you can confirm what was written; GET /v1/payroll/{id}/line-items itemises one.
- GET /v1/pay-frequency, GET /v1/users and GET /v1/custom-fields?type=payroll give the values this endpoint needs.

## Request body

- `pay_period_from` (string (date), **required**): Start of the pay period. Format `YYYY-MM-DD`. Send a real calendar date, because impossible dates such as 2026-02-30 still pass the format check. Example: `2026-02-20`.
- `pay_period_to` (string (date), **required**): End of the pay period. Format `YYYY-MM-DD`. Must be after `pay_period_from`. Example: `2026-02-28`.
- `pay_frequency` (string, **required**): Pay frequency name as listed by GET /v1/pay-frequency, case-insensitive, for example `Weekly` or `Bi-Weekly`. Example: `WEEKLY`.
- `worker_type` (string, **required**): `1099` or `w2`, case-insensitive. Must be a worker type your company's positions use, and every user in the request must have it. One of: `1099`, `w2`. Example: `1099`.
- `users` (array of object, **required**): Users to process. Maximum 10 per request. Max items: 10.
  - `user_id` (integer, **required**): User id from GET /v1/users. A number or a string of digits. Example: `778`.
  - `commission_amount` (number, **required**): Commission amount for this user. Must be non-negative. Minimum: 0. Example: `200`.
  - `override_amount` (number, **required**): Override amount for this user. Must be non-negative. Minimum: 0. Example: `200`.
  - `salary` (number): Optional salary or wage amount, stored as `hourly_salary` on the stub. A missing, negative or non-numeric value is stored as `0` without an error. Minimum: 0. Example: `100`.
  - `custom_fields` (object): Optional map of payroll custom field names (exactly as GET /v1/custom-fields?type=payroll lists them) to values. Only the fields you send are written.

Example (two_users): Two users with salary and custom fields

```json
{
  "pay_period_from": "2026-02-20",
  "pay_period_to": "2026-02-28",
  "pay_frequency": "WEEKLY",
  "worker_type": "1099",
  "users": [
    {
      "user_id": 778,
      "salary": 100,
      "commission_amount": 200,
      "override_amount": 200,
      "custom_fields": {
        "Sales Bonus": "100",
        "Expenses": "20"
      }
    },
    {
      "user_id": 779,
      "commission_amount": 200,
      "override_amount": 300,
      "custom_fields": {
        "Sales Bonus": "200",
        "Expenses": "40"
      }
    }
  ]
}
```

## Example request

```bash
curl -X POST "https://marketplace-api.sequifi.com/v1/payroll" \
  -H "Authorization: Bearer $SEQUIFI_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"pay_period_from":"2026-02-20","pay_period_to":"2026-02-28","pay_frequency":"WEEKLY","worker_type":"1099","users":[{"user_id":778,"salary":100,"commission_amount":200,"override_amount":200,"custom_fields":{"Sales Bonus":"100","Expenses":"20"}},{"user_id":779,"commission_amount":200,"override_amount":300,"custom_fields":{"Sales Bonus":"200","Expenses":"40"}}]}'
```

## Responses

### 200: The request was accepted. Each user has its own result in `data.results[]`; check every one, because all of them can fail.

- `status` (integer): The HTTP status code, repeated. Example: `200`.
- `message` (string): Short text. Wording varies by endpoint; branch on the HTTP status, not on this text. Example: `success`.
- `data` (object): The endpoint's payload. `{}` on most errors.

Example (success): Both users processed successfully

```json
{
  "status": 200,
  "message": "success",
  "data": {
    "requested_by": "acme",
    "results": [
      {
        "user_id": 778,
        "status": true,
        "message": "Payroll created successfully."
      },
      {
        "user_id": 779,
        "status": true,
        "message": "Payroll updated successfully."
      }
    ]
  }
}
```

Example (partial_failure): One user failed (per-user error)

```json
{
  "status": 200,
  "message": "success",
  "data": {
    "requested_by": "acme",
    "results": [
      {
        "user_id": 778,
        "status": true,
        "message": "Payroll created successfully."
      },
      {
        "user_id": 779,
        "status": false,
        "message": "User 779 not found."
      },
      {
        "user_id": 780,
        "status": false,
        "message": "User 780 does not belong to worker_type \"1099\". User's worker_type is \"w2\"."
      },
      {
        "user_id": 781,
        "status": false,
        "message": "No organization history found for user_id 781 with effective_date <= 2026-02-28"
      }
    ]
  }
}
```

### 400: Validation error (request-level)

Example (missing_fields): Required fields missing

```json
{
  "status": 400,
  "message": "Validation Failed",
  "data": {
    "pay_period_from": "Required. Format: YYYY-MM-DD",
    "pay_frequency": "Required."
  }
}
```

### 401: Missing, invalid or expired token. The gateway rejects the call before it reaches Sequifi, so the body has only a `message`.

```json
{
  "message": "Unauthorized"
}
```

### 500: Internal server error

```json
{
  "status": 500,
  "message": "Internal Server Error",
  "data": {}
}
```
