Wednesday, April 2, 2025

Genesys: Creating TTS prompts via API using HTTP only, no libs

The Uploading Architect Prompts with API document shows how to create prompts using either the JavaScript or the Python library. If you're lazy like me and/or want to avoid using a library, here's how create - TTS - prompts using simple HTTP: 

0. Get the auth token. 

1. Create a blank Prompt object: POST an object as JSON with the following fields: 

- String name,

- (Optional) String description

at https://api.mypurecloud.com/api/v2/architect/prompts

Save the selfUri value from the object returned by the server. 

2. Add a resource to the Prompt created at step 1: POST an object: 

- String language - from the list of supported languages, eg. "en-us",

- String text - the text of the prompt,

- String ttsString - use the same value as for text

at https://api.mypurecloud.com${uri}/resources where uri is the value of selfUri from step 1. 

 

Example methods - Groovy + Java 11:

Step 1: 

final def createPrompt(String accessToken, String body) {
 HttpRequest httpRequest = HttpRequest.newBuilder()
  .uri(new URI("https://api.mypurecloud.com/api/v2/architect/prompts"))
  .headers("Content-Type",  "application/json")
  .headers("Authorization", "Bearer $accessToken")
  .timeout(Duration.of(60, ChronoUnit.SECONDS))
  .POST(HttpRequest.BodyPublishers.ofString(body))
  .build()
  HttpResponse<String> httpResponse = HttpClient.newHttpClient().send(httpRequest, HttpResponse.BodyHandlers.ofString()) 
  def statusCode = httpResponse.statusCode()
  println("statusCode:\t$statusCode")
  println("httpResponse:\t${httpResponse.body()}")
 def jsonSlurper = new JsonSlurper()
 def object = jsonSlurper.parseText(httpResponse.body())
 println object
 return object
}

Where accessToken is the access token, and the body is

def o = [name: promptName, description: promptDescription]
def body = groovy.json.JsonOutput.toJson(o)

Step 2: 


final def addTTStoPrompt(String accessToken, String uri, body) {
 HttpRequest httpRequest = HttpRequest.newBuilder()
  .uri(new URI("https://api.mypurecloud.com${uri}/resources"))
  .headers("Content-Type",  "application/json")
  .headers("Authorization", "Bearer $accessToken")
  .timeout(Duration.of(60, ChronoUnit.SECONDS))
  .POST(HttpRequest.BodyPublishers.ofString(body))
  .build()
  HttpResponse<String> httpResponse = HttpClient.newHttpClient().send(httpRequest, HttpResponse.BodyHandlers.ofString()) 
  def statusCode = httpResponse.statusCode()
  println("statusCode:\t$statusCode")
  println("httpResponse:\t${httpResponse.body()}")
 def jsonSlurper = new JsonSlurper()
 def object = jsonSlurper.parseText(httpResponse.body())
 println object
 return object
}

uri is the selfUri value from step 1 and the body:

def o = [language: "en-us", text: ttsString, ttsString: ttsString ]
def body = groovy.json.JsonOutput.toJson(o)