building a Terraform module to automate post-interaction workflows, and we’ve hit a snag with programmatically assigning wrap-up codes. The requirement is to set the wrap-up code immediately after an interaction ends, specifically for voice interactions handled by agents.
I’ve been examining the Conversations API documentation, and the POST /api/v2/conversations/voice/{conversationId}/wrapup endpoint seems to be the correct place to apply these codes. However, the interaction must be in a specific state for this call to succeed. The documentation mentions that the conversation should be in the WRAPPED or CLOSED state, but our automated tests are returning a 409 Conflict error when we attempt to call this endpoint right after the agent clicks ‘Complete’ in the UI.
Here is the JSON payload we are sending:
{
"wrapUpCode": "Customer_Support_Closed",
"wrapUpCodeId": "123e4567-e89b-12d3-a456-426614174000"
}
We are authenticating using a service account with the conversation:voice:write scope. The error response body indicates that the conversation is not in a valid state for wrap-up. We suspect there might be a race condition where the API hasn’t fully transitioned the conversation to the WRAPPED state yet.
Is there a recommended polling mechanism or a webhook event we should listen to before making this API call? Alternatively, is there a different endpoint or method to ensure the wrap-up code is applied correctly without manual intervention? We’ve tried waiting for a few seconds, but it’s not reliable in a production environment. Any insights on handling this state transition would be appreciated.