CXone Studio Snippet: GetRESTProxy() throwing INVALID_FUNCTION error

Hitting a wall with a Studio Snippet that needs to call an external REST API. The goal is simple: grab a token, then fetch user data. The first call works fine, but the second one fails immediately with INVALID_FUNCTION.

Here’s the snippet logic:

ASSIGN tokenResponse = GetRESTProxy("https://auth.example.com/token", "POST", {"grant_type": "client_credentials"}, {}, {})
ASSIGN token = tokenResponse.body.access_token

ASSIGN headers = {"Authorization": "Bearer " + token}
ASSIGN userData = GetRESTProxy("https://api.example.com/user/123", "GET", {}, headers, {})

The error log points specifically to the second GetRESTProxy call. The headers object looks valid, and I’ve double-checked the token value in the debug trace-it’s not null or empty.

I suspect the issue is how the headers parameter is being passed. The docs for GetRESTProxy are sparse on the exact JSON structure expected for the headers argument in Studio Snippets. Is it expecting a JSON string or an object literal?

If I pass it as a stringified JSON, I get a different error (INVALID_ARGUMENT). If I pass it as an object, I get INVALID_FUNCTION.

Also, noticed that the first call works with empty headers {}. Maybe the SDK or Studio runtime is choking on the dynamic header construction.

Anyone else hit this? Is there a specific syntax for passing headers in GetRESTProxy that isn’t documented? Or is this a known bug in the Studio engine?

Tried restarting the snippet, no change. Tried hardcoding the header values, still fails.

Need a workaround or the correct syntax. The current approach is blocking deployment.

Docs state: “The GetRESTProxy function requires the headers parameter to be a valid JSON object containing string keys and string values.” You’re getting INVALID_FUNCTION because you’re likely passing the token variable directly without ensuring it’s a string, or the object structure is malformed before the second call.

The first call works because the payload is static. The second call fails because tokenResponse.body.access_token might be null if the first call didn’t return exactly what you expect, or the headers object isn’t constructed correctly in the snippet environment.

Here is the correct pattern. Note the explicit string handling and the separate assignment for the headers object to avoid scope issues.

// First, get the token
ASSIGN tokenResp = GetRESTProxy("https://auth.example.com/oauth/token", "POST", {"grant_type": "client_credentials"}, {"Authorization": "Basic " + EncodeBase64("clientId:clientSecret")}, {})

// Check if we actually got a token
IF tokenResp.status == 200 THEN
 ASSIGN accessToken = tokenResp.body.access_token
 
 // Construct headers explicitly as strings
 ASSIGN authHeaders = {"Authorization": "Bearer " + accessToken, "Content-Type": "application/json"}
 
 // Make the second call
 ASSIGN userData = GetRESTProxy("https://api.example.com/user/me", "GET", {}, authHeaders, {})
ELSE
 ASSIGN userData = {"error": "Token fetch failed"}
END IF

The issue is usually that GetRESTProxy in Studio Snippets is strict about the header format. If accessToken is empty or null, the "Bearer " + null concatenation can sometimes trip up the parser depending on the Studio version. Always validate the status code of the first call before attempting the second. Also, ensure the external URL is allowed in your NICE CXone security settings, otherwise it throws a generic function error instead of a network error.