C# Samples

This sample loads last 1000 messages for a specified device using username and password to authenticate.

using System;
using System.Collections.Generic;
using System.Net;
// use NuGET to add Json.NET library
using Newtonsoft.Json;

namespace ApiDemo
{
    class Program
    {
        private const string serverUri = "https://api.globalalerting.com";
        private const string username = "user@domain.com";
        private const string password = "mypassword";
        private const string tenantCode = "1234";
        private const string deviceId = "my-device";

        static void Main(string[] args)
        {
            string resourceUrl = String.Format("/V2/{0}/devices/{1}/momessages", tenantCode, deviceId);
            List<MoMessage> moMessages = Get<List<MoMessage>>(resourceUrl);

            foreach (var moMessage in moMessages)
                Console.WriteLine("{0} {1}", moMessage.SentFromDevice, moMessage.Message);
        }

        private static T Get<T>(string resourceUrl)
        {
            WebClient webClient = new WebClient();
            // pass username and password
            webClient.Credentials = new NetworkCredential(username, password);
            webClient.BaseAddress = serverUri;

            string json = webClient.DownloadString(resourceUrl);

            T data = JsonConvert.DeserializeObject<T>(json);

            return data;
        }

        #region Models

        public class MoMessage
        {
            public EmergencyState EmergencyState { get; set; }
            public string Message { get; set; }
            public string MessageId { get; set; }
            public string MessageType { get; set; }
            public Position Position { get; set; }
            public DateTime ReceivedAtServer { get; set; }
            public string Recipients { get; set; }
            public DateTime SentFromDevice { get; set; }
            public int SequenceNumber { get; set; }
            public Dictionary<string, string> Properties { get; set; }
            public List<ContainingGeofence> ContainingGeofences { get; set; }
            public string MimeType { get; set; }
            public string Filename { get; set; }
            public string ETag { get; set; }
        }

        public class Position
        {
            public double? Latitude { get; set; }
            public double? Longitude { get; set; }
            public DateTime? GpsTimestamp { get; set; }
            public double? Speed { get; set; }
            public double? Heading { get; set; }
            public double? Altitude { get; set; }
            public LocationSourceType LocationSource { get; set; }
            public string Accuracy { get; set; }
        }

        public class ContainingGeofence
        {
            public string GeofenceId { get; set; }
            public string GeofenceName { get; set; }
        }

        public enum LocationSourceType
        {
            Gps, Glonass, Doppler, Unknown, DGpsLt10m, GnssGt10m, AndroidLocationService, 
            iOSLocationService, Beacon, BDAS, Beidou, Galileo, UserProvided
        }

        public enum EmergencyState
        {
            None, Emergency, EmergencyCancel, NotSpecified
        }

        #endregion
    }
}

This sample loads device information using API key to authenticate.

using System;
using System.Collections.Generic;
using System.Net;
// use NuGET to add Json.NET library
using Newtonsoft.Json;

namespace ApiDemo
{
    class Program
    {
        private const string serverUri = "https://api.globalalerting.com";
        private const string apiKey = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ==-";
        private const string tenantCode = "1234";
        private const string deviceId = "my-device";

        static void Main(string[] args)
        {
            string resourceUrl = String.Format("/V2/{0}/devices/{1}", tenantCode, deviceId);
            Device device = Get<Device>(resourceUrl);

            Console.WriteLine("{0} {1}", device.FriendlyName, device.DeviceId);
        }

        private static T Get<T>(string resourceUrl)
        {
            WebClient webClient = new WebClient();
            // pass API key
            webClient.Headers.Add("X-GAPAPIKEY", apiKey);
            webClient.BaseAddress = serverUri;

            string json = webClient.DownloadString(resourceUrl);

            T data = JsonConvert.DeserializeObject<T>(json);

            return data;
        }

        #region Models

        public class Device
        {
            public string TenantCode { get; set; }
            public string DeviceId { get; set; }
            public string DeviceType { get; set; }
            public string FriendlyName { get; set; }
            public string Description { get; set; }
            public string Username { get; set; }
            public string UserId { get; set; }
            public string DeviceStatus { get; set; }
            public ProvisioningStatus? ProvisioningStatus { get; set; }
            public DateTime? DateAdded { get; set; }
            public string GroupId { get; set; }
            public bool? CheckInScheduleEnabled { get; set; }
            public OverdueState CheckInScheduleOverdue { get; set; }
            public bool EnableEmailDelivery { get; set; }
            public bool? EnableSmsReplies { get; set; }
            public bool IsMonitored { get; set; }
            public int? MonitoringIntervalMinutes { get; set; }
            public int? MonitoringBufferMinutes { get; set; }
            public OverdueState MonitoringOverdue { get; set; }
            public EmergencyState EmergencyState { get; set; }
            public string PhoneNumber { get; set; }
            public string ImageId { get; set; }
            public double? LastLatitude { get; set; }
            public double? LastLongitude { get; set; }
            public DateTime? LastMessageTime { get; set; }
            public DateTime? LastLocationTime { get; set; }
            public Dictionary<string, string> Properties { get; set; }
            public DateTime? Timestamp { get; set; }
            public string ETag { get; set; }
            public string Photo { get; set; }
            public string CustomEmailAddress { get; set; }
            public string SecondaryDeviceId { get; set; }
        }

        public enum ProvisioningStatus
        {
            Provisioned, NotProvisioned, Suspended
        }

        public enum OverdueState
        {
            NotOverdue, Warning, Overdue
        }

        public enum EmergencyState
        {
            None, Emergency, EmergencyCancel
        }

        #endregion
    }
}

PHP Sample

This sample loads last 1000 messages for a specified device.

#!/usr/bin/php
<?php 
    $auth="user@domain.com:mypassword";
    $device="my-device";
    $tenant="1234";
    $url = sprintf("https://api.globalalerting.com/V2/%s/devices/%s/momessages", $tenant, $device);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($curl, CURLOPT_USERPWD,$auth); 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response= curl_exec($curl);
    
    print $response;
?>

Python Samples

This sample loads list of devices using username and password to authenticate.

# compatible with Python 3.7+
import urllib.request
import base64
import json

username = 'user@domain.com'
password = 'mypassword'

server_uri = 'https://api.globalalerting.com'
tenant_code = '1234'

auth = base64.b64encode(bytes('{}:{}'.format(username, password), 'ascii'))
headers = {'Authorization': 'Basic {}'.format(auth.decode('utf-8')), 'Content-Type': 'application/json'}

# download Devices
resource_url = '/V2/{}/devices'.format(tenant_code)
request = urllib.request.Request(server_uri + resource_url, headers=headers)
response = urllib.request.urlopen(request)

devices = json.loads(response.read().decode('utf-8', errors='ignore'))
print(devices)

This sample loads last 1000 messages for a specified device using API key to authenticate.

# compatible with Python 3.7+
import urllib.request
import json

api_key = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ==-'

server_uri = 'https://api.globalalerting.com'
tenant_code = '1234'
device_id = 'my-device'

headers = {'X-GAPAPIKEY': api_key, 'Content-Type': 'application/json'}

# GET Devices
resource_url = '/V2/{}/devices'.format(tenant_code)
request = urllib.request.Request(server_uri + resource_url, headers=headers)
response = urllib.request.urlopen(request)

devices = json.loads(response.read().decode('utf-8', errors='ignore'))
print(devices)

# GET MoMessages
resource_url = '/V2/{}/devices/{}/momessages'.format(tenant_code, device_id)

request = urllib.request.Request(server_uri + resource_url, headers=headers)
response = urllib.request.urlopen(request)

mo_messages = json.loads(response.read().decode('utf-8', errors='ignore'))
print(mo_messages)