> For the complete documentation index, see [llms.txt](https://flapjax.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://flapjax.gitbook.io/docs/rest-api/reference/record-events/upsert-record-by-external-id.md).

# Upsert Record by External ID

## Upsert record by external ID

> Creates the record if no record with the given \`external\_id\` exists in this stack; otherwise updates the existing record. Returns \*\*201\*\* on create and \*\*200\*\* on update.<br>

```json
{"openapi":"3.0.3","info":{"title":"Listener API","version":"1.0.0"},"tags":[{"name":"upsert-record"}],"servers":[{"url":"https://{domain}/v1","variables":{"domain":{"default":"your-domain","description":"Your deployment domain"}}}],"security":[{"bearerAuth":[]}],"components":{"securitySchemes":{"bearerAuth":{"type":"http","scheme":"bearer","bearerFormat":"JWT","description":"JWT token with the required scope for the endpoint. Pass via the `Authorization: Bearer <token>` header.\n"}},"parameters":{"StackKey":{"name":"stack_key","in":"path","required":true,"description":"The unique key identifying the stack.","schema":{"type":"string"}},"RecordExternalId":{"name":"external_id","in":"path","required":true,"description":"The external identifier of the record.","schema":{"type":"string"}}},"schemas":{"Record":{"type":"object","description":"A record object within a stack.","properties":{"record_id":{"type":"string","description":"Internal MongoDB ObjectID."},"external_id":{"type":"string","description":"External identifier."}},"additionalProperties":{"description":"Attribute values as defined by the stack's schema."}},"ErrorResponse":{"type":"object","description":"Standard error response envelope.","properties":{"error":{"type":"string","description":"Machine-readable error type. Use this for programmatic error handling.\n","enum":["operation_failed","failed_binding_json","invalid_json","invalid_parameter","not_found","duplicate_entry","rate_limit_exceeded"]},"errorstack":{"type":"object","nullable":true,"description":"Detailed error stack (only present for server errors)."}}}}},"paths":{"/stacks/{stack_key}/upsert/{external_id}":{"put":{"summary":"Upsert record by external ID","operationId":"upsertRecordByExternalId","tags":["upsert-record"],"description":"Creates the record if no record with the given `external_id` exists in this stack; otherwise updates the existing record. Returns **201** on create and **200** on update.\n","parameters":[{"$ref":"#/components/parameters/StackKey"},{"$ref":"#/components/parameters/RecordExternalId"}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Record"}}}},"responses":{"200":{"description":"Existing record updated."},"201":{"description":"New record created."},"400":{"description":"Invalid request body.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"404":{"description":"Stack not found.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}}}}
```
