Get Started with Go

    Sign up for a Plivo Account

    When you sign up with Plivo, you will start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and purchase a number by clicking here. Add a number and credits to your account to start testing the full range of our voice and SMS service features.

    Sign up here to get your free Plivo account today.

    Follow these steps to successfully sign up for a free trial account:

    1. Sign up with your work email address
    2. Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
    3. Enter your mobile number to complete the phone verification step.

    Sign up with your work email address

    Sign up with email addres

    If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.

    Install Go and the Plivo Go Server SDK

    You must set up and install Go and Plivo’s Go SDK to make your first call. Here’s how.

    Install Go

    You can install Go from the Official Installer.

    Install Plivo Go Package

    • Create a project directory, run the following command:

      $ mkdir mygoapp
      
    • Change the directory to our project directory in the command line:

      $ cd mygoapp
      
    • You can install the Plivo Go package using the go command.

      $ go get github.com/plivo/plivo-go
      
    • You can also install by cloning this repository into your GOPATH.

    Make your first Outbound Call

    This section will guide you through how to use Plivo APIs to make voice calls from your application. First, let’s make sure you meet these prerequisites before we dive into the code.

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth ID and Auth Token on the home screen of your Plivo Console. Click here to sign up for a Plivo account if you haven’t already.

    credentials

    • Plivo Phone Number: You must have a voice-enabled Plivo phone number to receive incoming calls. You can purchase numbers from the numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Buy New Number

    • Answer Url: When a call is answered by the destination_number, you can control the call flow with the help of the answer_url set in the API request. Plivo will invoke the answer_url specified as soon as the call is answered and expect a valid XML response with instructions to handle the call. For example, you can use https://s3.amazonaws.com/static.plivo.com/answer.xml as answer_url to test your first outgoing call. The XML response of this is:

      <Response>
      <Speak>Congratulations! You've made your first outbound call!</Speak>
      </Response>
      

      As you can tell, the XML above holds instructions to say ‘Congratulations! You’ve made your first outbound call!’ to the callee. You can find the entire list of Plivo XML verbs and their details in the XML Reference section of the website here.

    Now, create a file called MakeCall.go and paste the following code:

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    package main
    
    import "fmt"
    import "github.com/plivo/plivo-go"
    
    func main() {
    	client, err := plivo.NewClient("", "", &plivo.ClientOptions{})
    	if err != nil {
    		panic(err)
    	}
    	response, err := client.Calls.Create(
    		plivo.CallCreateParams{
    			From: "Caller_ID",
    			To: "Destination_Number",
    			AnswerURL: "http://s3.amazonaws.com/static.plivo.com/answer.xml",
    			AnswerMethod: "GET",
    		},
    	)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Printf("Response: %#v\n", response)
    }
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • You can use os.Setenv & os.Getenv function to store environment variables and fetch them while initializing the client.
    • Replace the placeholder Caller_ID with the Phone number which you have purchased and Destination_Number with the phone number you will be making call to
    • Both Caller_ID and Destination_Number should be in E.164 format, for example +15671234567.
    • Destination_Number can also be a SIP endpoint. In case you are calling a SIP endpoint, the Destination_Number placeholder should be a valid SIP URI. Example: sip:john1234@phone.plivo.com

    save the file and use the below command to run it.

    go run MakeCall.go
    
    Note: If you are using a Plivo Trial account for this example, you can only make calls to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Set up Go Server for Incoming Calls & Callbacks

    In this section, we’ll walk you through how to set up a Go server in under five minutes and start handling incoming calls & callbacks.

    When a call is received on a Plivo voice-enabled number, you can control the call flow by declaring an answer URL for your Plivo application associated with that Plivo phone number. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call. We will see how to create and set up a new Plivo Application through the Plivo console in the following section.

    Notice how the concept of Answer URLs applies to both outbound API calls as well as incoming calls to your Plivo numbers. In the outbound API call example above, we specified the answer URL along with the make call API request, whereas in the case of incoming calls to Plivo numbers, the answer URL is specified in the Plivo application associated with the phone number.

    In addition to requests to the answer URL, Plivo initiates HTTP requests to your application server through the course of a call based on the specific XML elements in your answer XML. Such requests can be broadly classified into two categories:

    Action URL requests: XML instructions to carry forward the call are expected in response to these requests. These requests are typically invoked at the end of an XML element’s execution. For example: when an IVR input is received from the caller during a GetInput XML execution.

    Callback URL requests: No XML instructions are expected in response to these requests. Such requests serve as webhooks to notify your application server of important events through the course of an XML element’s execution. For example: when a conference participant is muted or unmuted.

    Set up a Go Server

    Use the following code snippet to start a local server.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"github.com/plivo/plivo-go"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
    
    	response := xml.ResponseElement{
    		Contents: []interface{} {
    			new(xml.SpeakElement).
    			SetContents("Congratulations! You've made your first outbound call!"),
    		},
    	}
    	fmt.Printf(response.String())
    
    	print("Message Received - ", fromnumber, " ", tonumber, " ", text)
    }
    
    func main() {
    	http.HandleFunc("/receive_call/", handler)
    	http.ListenAndServe(":8080", nil)
    }
    

    Save this code in any file (name the file something like receive_call.go). To run this file on the server, go to the folder where this file resides and use the following command:

      go run receive_call.go
    

    And you should see your basic server app in action on http://localhost:8080/receive_call/

    Exposing your local server to the internet

    To receive Incoming Calls and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:

    ./ngrok http <port_on_which_your_local_server_is_running>
    

    This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

    ngrok

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application. You can also use Plivo’s Application API.
    2. Give your application a name. Let’s call it Receive_call. Enter your server URL (e.g. http://example.com/receive_call/) in the Answer URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create to save your application.

    Create Application

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for this app.
    2. Select Receive Call (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Receive Call App

    Test and validate

    Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL requesting for a valid XML response and process the call. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.

    Forward an Incoming call

    To forward an incoming call, you need to use Dial XML.

    • You should set up the dial XML document in your server
    • URL of the dial XML in your server has to be set up as the answer_url of the app assigned to the Plivo number.
    • During an incoming call to your Plivo number, Plivo will send a request to the answer_url expecting a valid XML to process the incoming call.
    • As the response would be a dial XML, the call will be forwarded to the number defined in the XML response.
    Note: Phone number must be a Plivo phone number that is Voice-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting Voice in the features option to buy Voice enabled numbers.

    Create a file called forward_call.go and paste the below code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    package main
    
    import (
    	"net/http"
    	"plivo-go/xml"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
    	response := xml.ResponseElement{
    		Contents: []interface{}{
    			new(xml.DialElement).
    				SetContents(
    					[]interface{}{
    						new(xml.NumberElement).
    							SetContents("15671234567"),
    					},
    				),
    		},
    	}
    	w.Write([]byte(response.String()))
    }
    
    func main() {
    	http.HandleFunc("/forward_call/", handler)
    	http.ListenAndServe(":8080", nil)
    }
    
    Note: Create an application in the Plivo console with your Server URL in the answer_url field and assign the app to a Plivo number.

    Test and validate

    Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL requesting for a valid XML response and process the call. Once Plivo receives the dial XML response, the call will be forwarded to the number defined in the XML. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.