DEV Community

Cover image for How To Make and Receive calls using any Programming language ?
Ameed Jamous
Ameed Jamous

Posted on

1 1

How To Make and Receive calls using any Programming language ?

Programmable Voice API

Learn how to make and receive phone calls programmatically using Restcomm API (Twilio Alternative) where you can use / bring your own carrier.

A Call represents a connection between a phone or user agent and Restcomm CPaaS. This may be inbound or outbound. The Calls list resource represents the set of phone calls originated and terminated from an account.


curl -X POST https://cloud.restcomm.com/restcomm/2012-04-24/Accounts/ACCOUNT_SID/Calls.json  \
   -d 'From=16175551212' \
   -d 'To=16172221212' 
   -d 'Url=https://ACCOUNT_SID:AUTH_TOKEN@mycompany.com/restcomm/demos/hello-play.xml' \
   -u 'YourAccountSid:YourAuthToken'


Enter fullscreen mode Exit fullscreen mode

Making and receiving a call in JAVA

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.util.Base64;

public class JavaSampleClass {
   // Provide your Account Sid and Auth Token from your Console Account page
   public static final String ACCOUNT_SID = "my_ACCOUNT_SID";
   public static final String AUTH_TOKEN = "my_AUTH_TOKEN";


   public static void main(String[] args) throws Exception {
      String userAndPass = ACCOUNT_SID + ':' + AUTH_TOKEN;
      String encoded = Base64.getEncoder().encodeToString(userAndPass.getBytes());

      URL url = new URL(("https://cloud.restcomm.com/restcomm/2012-04-24/Accounts/" + ACCOUNT_SID + "/Calls.json");
      HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
      conn.setRequestProperty("Authorization", "Basic " + encoded);
      conn.setRequestMethod("POST");
      conn.setDoOutput(true);
      DataOutputStream os = new DataOutputStream(conn.getOutputStream());

      // Update POST parameters accordingly
      os.writeBytes("From=16175551212&" +
        "To=16172221212" +
       "Url=https://ACCOUNT_SID:AUTH_TOKEN@mycompany.com/restcomm/demos/hello-play.xml");
      os.close();

      // Add your business logic below; response code can be obtained from 'conn.getResponseCode()' and input stream from 'conn.getInputStream()'
      ...
  }
}

Enter fullscreen mode Exit fullscreen mode

You can also provide a list of events you are interested in getting notified on and provide a URL to receive them at. Here’s an example for receiving answered and completed events. The callbacks will be made at http://status.callback.url


curl -X POST https://cloud.restcomm.com/restcomm/2012-04-24/Accounts/ACCOUNT_SID/Calls.json  \
   -d 'From=16175551212' \
   -d 'To=16172221212' \
   -d 'Url=https://ACCOUNT_SID:AUTH_TOKEN@mycompany.com/restcomm/demos/hello-play.xml' \
   -d 'StatusCallbackEvent=answered,completed' \
   -u 'YourAccountSid:YourAuthToken'

Enter fullscreen mode Exit fullscreen mode

For detailed and updated docs, visit https://cloud.restcomm.com/docs/api/calls-api.html

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay