How to set wrap-up code programmatically via API after interaction ends?

We’re building a custom agent desktop using the Genesys Cloud JavaScript SDK. The goal is to automate wrap-up codes based on interaction metadata. When the conversation ends, we trigger a backend process that determines the correct code.

I’m trying to set this via the API. I found the endpoint:
POST /api/v2/conversations/interactions/{interactionId}/wrap-up

The documentation says to send a JSON body with the wrapUpCodeId. Here’s what I’m sending:

{
 "wrapUpCodeId": "12345678-1234-1234-1234-123456789012"
}

But I keep getting a 409 Conflict response. The error message says “Interaction is not in a valid state for wrap-up.”

The interaction status shows as closed in the UI. I’m assuming the API expects the interaction to be in a specific state before applying the code. I’ve tried waiting 2-3 seconds after the closed event fires, but it still fails.

Is there a specific state check I need to perform? Or is there a different API call to force the wrap-up? I can’t find clear examples in the SDK docs for this exact flow. Any pointers would be appreciated.