Search code examples
text-to-speechvonage

Nexmo API change on Text to Speech?


For a very long time I've been using the following to send text-to-speech alerts from my applications.

curl 'https://api-us-1.nexmo.com/tts/json' \
-d api_key=****** \
-d api_secret=****** \
-d to=0035193xxxxxxx \
-d from=0035193xxxxxxx \
--data-urlencode 'text=Alert! Check Something... ' \
-d repeat=2 \
-d voice="male" \

Very recently the service has stopped working for some carriers.
While going over Nexmo docs I can't see the /tts/json API documented.

Anyone knows what happened?
Is the /tts/json API still usable?
The /v1/calls API is absolutelly overkill for my needs.


Solution

  • Unfortunately that API was sunset quite a while ago and replaced with the newer Voice API.

    https://developer.nexmo.com/voice/voice-api/code-snippets/make-an-outbound-call-with-ncco would the closest alternative with the Voice API. The biggest change is switching to using a JWT for authentication versus the key/secret auth the older API used.

    If you have the Nexmo CLI installed you can generate a JWT as part of a script. The following should work:

    #!/bin/bash
    #
    # Send voice message to a user
    #
    # ./script.sh <number to call> <vonage number> "<message to speak>"
    
    PATH_TO_PRIVATE_KEY=<path to private key>
    VONAGE_APPLICATION_ID=<application ID>
    TO_NUMBER=$1
    VONAGE_NUMBER=$2
    MESSAGE=$3
    JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$VONAGE_APPLICATION_ID)
    
    curl -X POST https://api.nexmo.com/v1/calls\
        -H "Authorization: Bearer "$JWT\
        -H "Content-Type: application/json"\
        -d "{\"to\":[{\"type\": \"phone\",\"number\": \"$TO_NUMBER\"}],
            \"from\": {\"type\": \"phone\",\"number\": \"$VONAGE_NUMBER\"},
            \"ncco\": [
              {
                \"action\": \"talk\",
                \"text\": \"$MESSAGE\"
              }
            ]}"