CXone Studio: 409 Conflict when setting wrap-up code via REST Proxy after interaction ends

I’m hitting a wall trying to set a wrap-up code programmatically for a chat interaction right after it terminates. The flow is straightforward. Interaction ends. I use the GetRESTProxy action to call the Conversations API. The goal is to attach a specific wrap-up code to the interaction for reporting purposes.

I’m using the endpoint:
POST /api/v2/conversations/interactions/{interactionId}/wrapup

The interactionId is pulled from the session context. The JSON body looks like this:

{
 "wrapupCode": {
 "id": "the-static-wrapup-code-id"
 }
}

The issue is the timing. If I put this REST Proxy call in the flow immediately after the “End Interaction” action, it returns a 409 Conflict. The error message says the interaction is still in a “connected” state, even though the flow has moved past the disconnect action.

I tried adding a 5-second delay using the “Wait” action. It worked locally in the debug mode sometimes, but in production, it’s flaky. Sometimes it’s too fast, sometimes the interaction is already closed and it throws a different error saying the interaction isn’t active anymore.

I also tried using the PUT /api/v2/conversations/interactions/{interactionId} endpoint to update the metadata, but the wrap-up field seems read-only or ignored if the interaction isn’t in the right state.

Is there a reliable way to hook into the interaction lifecycle to set this code? Or is the REST Proxy just not the right tool here? I need this to happen automatically without agent intervention. The current flow feels like a race condition waiting to happen. I’ve checked the documentation on interaction states, but it’s vague on the exact millisecond the state flips from “connected” to “disconnected” to “closed”. Any ideas on how to stabilize this?