---
title: "Set a value with optional expiration"
method: POST
path: "/kv/set"
tags: ["KV Store"]
---

# Set a value with optional expiration

`POST /kv/set`

## Request

- Base URL: `/api` (relative, and the document was published on a file host, not at the API’s origin)
- URL: `/api/kv/set`
- Auth: HTTP bearer

## Request body

- SetRequest: Request to set a value
  - `ex` integer, nullable: Expire in seconds
  - `key` string, required: The key to set
  - `nx` boolean: Only set if key does not exist
  - `project_id` integer, nullable: Project ID (required for API key/session auth, optional for deployment tokens)
  - `px` integer, nullable: Expire in milliseconds
  - `value` unknown, required
  - `xx` boolean: Only set if key exists

## Response `200`

Value set, or `result: null` when an NX/XX condition did not hold

- SetResponse: Response for set operation
  - `result` string, nullable: "OK" when the value was written; null when an NX/XX condition did not hold and the key was left unchanged (the Redis reply)

## Other responses

- `401`: Unauthorized
- `500`: Internal server error

## Changes

- **2026-11-26** `ec9326f3080e`: 2 breaking
  - the response property `result` became nullable for the status `200`
  - the response property `result` became optional for the status `200`
- **2026-04-25** `774be20905f2`: 1 info
  - endpoint added

[Operation history](https://skmtc.dev/gotempsh/apis/temps/changes/kv/set/post.md)

---

[API](https://skmtc.dev/gotempsh/apis/temps.md) - [All operations](https://skmtc.dev/gotempsh/apis/temps/llms.txt) - [OpenAPI document](https://skmtc.dev/gotempsh/apis/temps/revisions/3b040526eb43?raw)
