How to programmatically close a Web Messaging session from the backend using Python SDK

We’ve got a backend service running on an AWS EC2 instance that processes chat transcripts for compliance. Once the processing is done, we need to close the Web Messaging session so the agent can’t send more messages and the transcript is finalized. I’m using the Genesys Cloud Python SDK (genesyscloud v2.2.0) to handle this.

I found the endpoint /api/v2/conversations/messaging/sessions/{sessionId}/close in the docs. It requires a POST request. Here is the code I wrote to test this out:

from genesyscloud import ConversationApi
from genesyscloud.conversations.models import ConversationSessionCloseRequest

config = ConfigurationBuilder.build_from_default_profile()
auth = Authenticator(client_id, client_secret)
auth.login()
config.client = auth.get_client()

conversation_api = ConversationApi(config)
session_id = "some-valid-session-id-from-webhook"

try:
 request_body = ConversationSessionCloseRequest(
 close_reason="Compliance processing complete"
 )
 response = conversation_api.post_conversations_messaging_sessions_close(session_id, request_body)
 print("Session closed successfully:", response)
except Exception as e:
 print("Error closing session:", e)

When I run this, I get a 404 Not Found error. The error message says Resource not found. I know the session ID is valid because I just received it in the conversation:updated webhook event. I double-checked the ID format and it matches the examples in the docs.

Is this the right endpoint to use for closing a Web Messaging session? I saw some older posts mentioning using the Guest API, but that seems deprecated. Or do I need to use a different method in the SDK? Maybe I’m missing a required header or the session state needs to be something specific before it can be closed?

The session is currently in the connected state. I tried waiting a few seconds after the webhook arrived, but no luck. Any ideas on what I’m doing wrong here?