VB.net code examples for integration with LabsMobile SMS API
Below are the examples of programming code in VB.net language to send SMS messages through the LabsMobile platform API.
This documentation is designed for you to connect your applications with the LabsMobile platform and automate the sending of SMS messages. The main objective of the integration of these applications is the sending of SMS messages and related communications.
Advice We recommend using our API SMS http/POST in JSON format for any integration with the LabsMobile platform. But we have other API versions that you can use depending on your environment and requirements.
With these examples you will be able to perform the integration of the following functionalities:
- Send SMS messages individually or in bulk.
- Check your account balance.
- Receive delivery confirmation and/or error notifications corresponding to the messages sent.
- Obtain notification and data of the messages received in the virtual numbers contracted.
To be taken into account
The LabsMobile API SMS uses a common base URL for all requests: https://api.labsmobile.com
.
It is highly recommended to use a URL that includes the HTTPS
protocol in any version of the API.
Authentication is done with the username and an API token, myUsername:myToken
. You must create the API token from the API Settings section of your account.
Send SMS with http/POST
Here is an example of code in VB.net language to send SMS messages using the API SMS http/POST that uses the JSON
format for the information exchange variables.
As you can see, you need to create a structure in JSON
format containing all the necessary parameters for sending and make a HTTP/POST
call with this data in the body of the request.
Imports System Imports System.Net.Http Imports System.Text Module Module1 Sub Main() Dim userToken As String = "myUsername:myToken" Dim credentials As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(userToken)) Dim client As New HttpClient() Dim mediaType As String = "application/json" Dim jsonBody As String = "{ ""message"":""Your verification code is 123"", ""tpoa"":""Sender"", ""recipient"":[ { ""msisdn"":""12015550123"" } ] }" Dim content As New StringContent(jsonBody, Encoding.UTF8, mediaType) client.DefaultRequestHeaders.Add("Authorization", "Basic " & credentials) Dim request As New HttpRequestMessage(HttpMethod.Post, "https://api.labsmobile.com/json/send") request.Content = content Dim response As HttpResponseMessage = client.SendAsync(request).Result Dim responseContent As String = response.Content.ReadAsStringAsync().Result Console.WriteLine(responseContent) End Sub End Module
Imports System Imports System.Net.Http Imports System.Text Module Module1 Sub Main() Dim userToken As String = "myUsername:myToken" Dim credentials As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(userToken)) Dim client As New HttpClient() Dim mediaType As String = "application/json" Dim jsonBody As String = "{ ""message"":""Your verification code is 123"", ""tpoa"":""Sender"", ""recipient"":[ { ""msisdn"":""12015550123"" }, { ""msisdn"":""12015550124"" }, { ""msisdn"":""12015550125"" } ] }" Dim content As New StringContent(jsonBody, Encoding.UTF8, mediaType) client.DefaultRequestHeaders.Add("Authorization", "Basic " & credentials) Dim request As New HttpRequestMessage(HttpMethod.Post, "https://api.labsmobile.com/json/send") request.Content = content Dim response As HttpResponseMessage = client.SendAsync(request).Result Dim responseContent As String = response.Content.ReadAsStringAsync().Result Console.WriteLine(responseContent) End Sub End Module
{ "subid": "65f33a88ceb3d", "code": "0", "message": "Message has been successfully sent." }
{ "subid": "65f7f7041385d", "code": "35", "message": "The account has no enough credit for this sending" }
All error codes can be found in API documentation, error codes.
For more details on the available parameters and configuration options, see the official documentation at:
Send SMS with http/GET
This is an example of code in VB.net language to send SMS messages using the SMS API http/GET
.
As you can see, you must pass a series of GET
variables in the same URL and make a HTTP
call. It is important to encode all values as URL using the Uri.EscapeDataString()
function.
Important The SMS http/GET API transmits credentials (username and API token) unencrypted and unsecured. We recommend using this API GET
only when absolutely essential and use the API SMS http/POST instead.
Imports System Imports System.Net.Http Module Module1 Sub Main() Dim client As New HttpClient() Dim username As String = "my@Username" Dim token As String = "myToken" Dim msisdn As String = "12015550123" Dim message As String = "Your verification code is 123" Dim encodedUsername As String = Uri.EscapeDataString(username) Dim encodedToken As String = Uri.EscapeDataString(token) Dim encodedMsisdn As String = Uri.EscapeDataString(msisdn) Dim encodedMessage As String = Uri.EscapeDataString(message) Dim url As String = $"https://api.labsmobile.com/get/send.php?username={encodedUsername}&password={encodedToken}&msisdn={encodedMsisdn}&message={encodedMessage}" Dim response As HttpResponseMessage = client.GetAsync(url).Result Dim responseContent As String = response.Content.ReadAsStringAsync().Result Console.WriteLine(responseContent) End Sub End Module
Imports System Imports System.Net.Http Module Module1 Sub Main() Dim client As New HttpClient() Dim username As String = "myUsername" Dim token As String = "myToken" Dim msisdn As String = "[12015550123,12015550124,12015550125]" Dim message As String = "Don't miss our Sale! Use code XXXX123 for 20% off." Dim encodedUsername As String = Uri.EscapeDataString(username) Dim encodedToken As String = Uri.EscapeDataString(token) Dim encodedMsisdn As String = Uri.EscapeDataString(msisdn) Dim encodedMessage As String = Uri.EscapeDataString(message) Dim url As String = $"https://api.labsmobile.com/get/send.php?username={encodedUsername}&password={encodedToken}&msisdn={encodedMsisdn}&message={encodedMessage}" Dim response As HttpResponseMessage = client.GetAsync(url).Result Dim responseContent As String = response.Content.ReadAsStringAsync().Result Console.WriteLine(responseContent) End Sub End Module
{ "subid": "65f33a88ceb3d", "code": "0", "message": "Message has been successfully sent." }
{ "subid": "65f7f7041385d", "code": "35", "message": "The account has no enough credit for this sending" }
All error codes can be found in API documentation, error codes.
For more details on the available parameters and configuration options, see the official documentation at:
Account balance inquiry
With this code example in VB.net
you can consult your account balance using the LabsMobile API SMS
.
Through a call to this endpoint, you can get information about the amount of credits available in your LabsMobile account. The connection is established through a HTTP/GET
request with authentication in the HTTP
connection header.
Imports System Imports System.Net.Http Imports System.Text Module Module1 Sub Main() Dim userToken As String = "myUsername:myToken" Dim credentials As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(userToken)) Dim client As New HttpClient() client.DefaultRequestHeaders.Add("Authorization", "Basic " & credentials) Dim request As New HttpRequestMessage(HttpMethod.Get, "https://api.labsmobile.com/json/balance") Dim response As HttpResponseMessage = client.SendAsync(request).Result Dim responseContent As String = response.Content.ReadAsStringAsync().Result Console.WriteLine(responseContent) End Sub End Module
{"code":0,"credits":"10"}
See the complete documentation at:
Receive status of sent messages
This example script receives a call from the LabsMobile platform when a sent SMS message changes status. To implement this solution, it is essential to configure the appropriate parameters, the confirmation URL in the ackurl
parameter in the request or the default URL in the API Settings of your account.
Therefore, it is necessary to publish a script like this in your system so that our platform calls the URL when a status change occurs in any of the sent messages.
The example code obtains the URL parameters using the global $_GET
variable and then assigns these parameters to local variables.
Imports System Imports System.Collections.Specialized Imports System.Web Imports System.Net Module Program Sub Main(args As String()) MainRecieveMessageStatus(args) End Sub Async Sub MainRecieveMessageStatus(args As String()) Dim port As Integer = 3000 Dim listener As New HttpListener() listener.Prefixes.Add($"http://+:{port}/") listener.Start() Console.WriteLine($"The server is running on port {port}") While True Dim context = Await listener.GetContextAsync() Dim request = context.Request Dim response = context.Response Dim queryParams As NameValueCollection = If(request?.Url IsNot Nothing, HttpUtility.ParseQueryString(request.Url.Query), New NameValueCollection()) Dim acklevel As String = If(queryParams.Get("acklevel"), "default_value") Dim credits As String = If(queryParams.Get("credits"), "default_value") Dim msisdn As String = If(queryParams.Get("msisdn"), "default_value") Dim status As String = If(queryParams.Get("message"), "default_value") Dim subid As String = If(queryParams.Get("subid"), "default_value") Dim timestamp As String = If(queryParams.Get("timestamp"), "default_value") Console.WriteLine($"Variable acklevel: {acklevel}") Console.WriteLine($"Variable credits: {credits}") Console.WriteLine($"Variable msisdn: {msisdn}") Console.WriteLine($"Variable status: {status}") Console.WriteLine($"Variable subid: {subid}") Console.WriteLine($"Variable timestamp: {timestamp}") Dim responseString As String = "200 OK" Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString) response.ContentType = "text/plain" response.ContentLength64 = buffer.Length Await response.OutputStream.WriteAsync(buffer, 0, buffer.Length) response.Close() End While End Sub End Module
See the complete documentation at:
Receiving SMS messages
Once you have contracted a virtual number, you will be able to receive messages via API
on a specific URL via a HTTP/GET
call to a script in your system. Each message received will invoke the URL, transmitting all its data in variables in JSON
format.
To activate this functionality you must inform the URL for receiving messages in the Settings of your account.
It is necessary to configure an endpoint in your system so that the LabsMobile platform calls this script when an SMS is received on any of the contracted virtual numbers.
The example code obtains the URL parameters using the global variable $_GET
and then assigns these parameters to local variables.
Imports System Imports System.Collections.Specialized Imports System.Web Imports System.Net Module Program Sub Main(args As String()) MainRecieveMessageStatus(args) End Sub Async Sub MainRecieveMessageStatus(args As String()) Dim port As Integer = 3000 Dim listener As New HttpListener() listener.Prefixes.Add($"http://+:{port}/") listener.Start() Console.WriteLine($"The server is running on port {port}") While True Dim context = Await listener.GetContextAsync() Dim request = context.Request Dim response = context.Response Dim queryParams As NameValueCollection = If(request?.Url IsNot Nothing, HttpUtility.ParseQueryString(request.Url.Query), New NameValueCollection()) Dim inbound_number As String = If(queryParams.Get("inbound_number"), "default_value") Dim service_number As String = If(queryParams.Get("service_number"), "default_value") Dim msisdn As String = If(queryParams.Get("msisdn"), "default_value") Dim message As String = If(queryParams.Get("message"), "default_value") Dim timestamp As String = If(queryParams.Get("timestamp"), "default_value") Console.WriteLine($"Variable inbound_number: {inbound_number}") Console.WriteLine($"Variable service_number: {service_number}") Console.WriteLine($"Variable msisdn: {msisdn}") Console.WriteLine($"Variable message: {message}") Console.WriteLine($"Variable timestamp: {timestamp}") Dim responseString As String = "200 OK" Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString) response.ContentType = "text/plain" response.ContentLength64 = buffer.Length Await response.OutputStream.WriteAsync(buffer, 0, buffer.Length) response.Close() End While End Sub End Module
See the complete documentation at:
Support resources
We recommend that you consult and take into account the following support resources in your integration:
- Description, manual and code examples of the SMS sending API.
- Technical guide to an OTP validation or authentication process by SMS.
- All LabsMobile API versions and functionalities.
- First Steps to API Integration Tutorial
- Create a demo account
- Recommendations and best practices in any integration.
- Plugins, modules and extensions.
- Need help? Contact our technicians