Our Products & Services
Getting Started
First Time Setup
Getting Started With Jobs
Getting Started with Ticketing
Setting Sonar up for Billing
Getting Started with Accounts
Getting Started with Inventory
Baseline Configuration
User Specific Resources
How To: Using Sonar's Customer Portal
Accounts
Account Types: Overview & Example Use Cases
Account Statuses: Overview & Example Use Cases
Account Management View: Overview
Scheduled Events: Overview & Use Cases
Notes & Tasks: Best Practices & Use Cases
Child Accounts: Best Practices & How Tos
Disconnecting an Account
Understanding the Affordable Connectivity Program
FCC Broadband Data Collection (BDC) Filings: How Sonar Can Help
Account List View: Overview
Specify Account ID upon Creation
Lead Intake Form Processing
Account Groups: Overview & Example Use Cases
CPUC Fixed Broadband Deployment by Address
Serviceable Addresses: Overview and Usage
FCC Data Exports: General Overview and Usage
Creating a New Account
Billing
Setting up Bank Account & Credit Card Processors
Billing Settings
Billing Defaults
Taxes Setup
General Ledger Codes: Overview
Services: Overview
Building a Data Service
Building Packages
Delinquency Billing Best Practices
Accounts in Vacation Mode
Batch Payments & Deposit Slips: Overview
Creating Discounts for Services and Packages
Setting Up Payment Methods and Taking Payments
Canadian ACH tool
Printed Invoice Batches: Overview
Delinquency Exclusions: Overview and Use Cases
Multi-Month Billing & Multi-Month Services
How to Take Bank Account Payments
Email Invoice Batch: Overview
Manual Transactions
ACH Batching: Overview
Billing Calculator
General Transactions: Best Practices
Usage Based Billing Policies: Overview and Usage
How to: Adding a Service to an Account
Usage Based Billing Policy Free Periods: Overview and Usage
Using Tax Exemptions - How To
Avalara: Overview & Setup
Print to Mail
Communication
Setting up an Outbound Email Domain
Triggered Messages: Setup
Call Logs: General Best Practices
Using the Mass Message Tool
Saved Messages: Overview
Message Categories: Overview & Use Cases
Phone Number Types: Overview and Use Cases
Email Variables & Conditions
Communication Tools: Overview
Trigger Explanations
Companies
How to: Setting Up a Company in Sonar
Managing Multiple Companies in Sonar: Best Practices
Rebranding your Sonar Instance
Financial
Contract Templates
Invoice Attachment Use Cases & PDF Examples
Invoice Messages: Overview & Use Cases
Invoices in Sonar: Examples, Creation & Contents
Integrations
Calix Integration: Overview
GPS Tracking Providers: Overview
Webhooks in Sonar: Basic PHP Example
How to Connect Cambium to your Sonar Instance
iCalendar Integration
RemoteWinBox - Integration with Sonar
External Marketing Providers
The Sonar Field Tech App
How to Connect Preseem to your Sonar System
Tower Coverage Integration: Overview
GoCardless Integration: Overview & Setup
Vetro FiberMap Integration: Overview
How to: Using Webhooks in Sonar
CrowdFiber Integration
Inventory
Setup of Inventory: Manufacturers, Categories, and Assignees
Inventory List View: Overview
Inventory Model Management: General Overview
Tracking and Using Consumable Inventory
Jobs
Job Types: Best Practices
Setting Up Schedules General Overview
Applying Task Templates to Jobs
Scheduling Week View: Overview
Example Jobs & Templates
Scheduling How-to: Creating and Booking a Job
Geofences: Overview
Edit Job Options
Jobs and Scheduling: Overview
Mapping
Misc.
Monitoring
Building a Monitoring Template
Pollers: General Overview, Deployment Strategy, Build Out & Setup
Building Alerting Rotations
Poller Troubleshooting
Networking
IP Assignments & Sonar
MikroTik: Setting Up a Sonar Controlled DHCP Server
IPAM: Basic Setup
MikroTik as an Inline Device: Integration With Sonar
MikroTik: Controlling Speeds
MikroTik: Controlling Access
PacketLogic: Integration With Sonar
Setting Up CoA Proxy
RADIUS: Building Reply Attributes
Data Usage Available Methods
Pulse, Polling, and PHP
Using Multiple Network Devices in Sonar
IPAM: Overview
Cable Modem Provisioning
LTE Integration
Assigning RADIUS Addresses
Controlling Customer Speeds with Sonar: General Overview
Sonar Flow
RADIUS: Build-Out & Integration with Sonar
Network Dashboard: Overview
Being Cloud Native
Netflow Integration: Overview
Building a Device Mapper
Sonar IP Addressing
Automating IP Assignments, Data Rates, and Network Access in Sonar
DHCP Delivery
Assigning an IP Address Using Sonar's IPAM: How to
Network Sites: Management View Overview
Building RADIUS Groups
Building Address Lists
Finding your OIDs
Purchase Orders
Release Notes
Reporting
Understanding Sonar Reports
How To Enhance Your Reporting With Custom Field Data
Report Licenses
Sonar's Business Intelligence: Overview
Security
User Role Creation & Best Practices
Removing a Terminated Employee In Sonar
Password Policy In Depth
Application Firewall: General Overview and Best Practices
Users: Overview
Role Creation using GraphiQL
Sonar Billing
sonarPay
System
How to Best Use Global Search
How Your Data is Backed Up
How To Use GraphiQL to Understand the Sonar API
Frequently Used Terms
Sonar's Rich Text Editor
Mutations in the Sonar API
Notification Preferences
API Calls Using Third Party Applications: Personal Access Tokens
Auth0: Overview
The New Sonar API
Introducing the New Sidebar
Upgrading your Ubuntu OS - Customer Portal Upgrades
A Deeper Dive into the New Sonar API
Consuming the Sonar API
Filtering: Simple vs Advanced
Browser Compatibility and Minimum Hardware Requirements for Sonar
REST API Wrappers for V1 Compatibility
User Profile: Your Personal User Settings
SMS Notifications
Dynamic Time Zones in Sonar
System Settings: Overview
Troubleshooting the Customer Portal
Controlling Your Landing Page: Personal Preferences
Interacting with Files via the API
Getting Your Data into Sonar
Customizing Your Customer Portal
Ticketing
Ticketing: Overview
Canned Replies Examples & Templates
Canned Reply Categories
Inbound Mailboxes Example Build
Ticket Categories Best Practices & Example Build
Exploring Ticket Groups
Using Parent Tickets
How to Integrate Inbound Mailboxes with Slack
Advanced Ticketing Features
Voice
Working With the Sonar Team & Additional Resources
Sonar's Security Practices & Certifications
Sonar and General Data Protection Regulation (GDPR)
Technical Security Overview
CyberSecure Canada Certification
Sonar's Security Strategies
Best Practices for Fast Tracking a Support Request
How Sonar Bills Your Instance
The Sonar Status Page
The Sonar Community Forum
Feedback Portal / Suggest a Feature
Sonar Casts Table of Contents
How to Request Termination of your Sonar Instance
Submitting Bugs vs. Feature Requests
Third Party Customer Support Referrals
Client Services Training Overview
Learning with Sonar: Tools and Resources
Table of Contents
- All Categories
- System
- Consuming the Sonar API
Consuming the Sonar API
Updated
by Simon Westlake
Read Time: 5 mins
In the first three articles in this series, I walked you through how to use the API using GraphiQL, a web-based tool included in Sonar. Now let’s build a very simple application so we can consume the API externally.
Expand here to review the first 3 articles of the Sonar API series
Using the Sonar GraphQL API in an external application is pretty simple. It’s served over HTTPS, and the only HTTP verb you need to use is POST. GraphQL is a different paradigm to something like REST, where the HTTP verb dictates the behavior. In GraphQL, a query always fetches data and a mutation always manipulates data. As a query and a mutation can both take input data, we use a POST to submit to the server, and receive JSON in response.
Starting our app
I’m going to build this in PHP, but the concepts are applicable to almost any language. All you need is a way to make a HTTP request, and the ability to parse JSON. I’m going to use Guzzle as my HTTP client in this case.
First, let’s install Composer, the de-facto package manager for PHP. Next, I’m going to setup my simple app by typing composer init.

Now I can add Guzzle as a dependency by typing composer require guzzlehttp/guzzle.

Now I’m going to create a single PHP file called medium.php to act as my app. In this file, I’m going to include the Composer autoloader to automatically load any packages I install. I’ll also add a temporary echo statement so I can make sure it works.
<?php
require(__DIR__ . '/vendor/autoload.php');
echo 'Hello Medium!';
Now let’s test it out in my shell by typing php medium.php.

Alright, great — my file executes at least. Let’s move onto actually making an API request.
Calling the Sonar API
In order to access the API, you need an access token. The access token must be generated inside Sonar, so navigate to your profile page by clicking on the avatar in the top-right corner. By default, you'll reach the Overview page of your profile, click on Personal Access Tokens and then Create Personal Access Token.

In the modal that populates following this, input a name and then click Create.
Next, we need to start building up our request. Let’s start by making a simple query. I personally like to use heredoc syntax to write out my query.
<?php
require(__DIR__ . '/vendor/autoload.php');
$query = <<<GQL
query MyCoolQuery {
accounts {
entities {
id
name
created_at
}
}
}
GQL;
Here, I’ve wrapped my simple query inside heredoc syntax using <<<GQL. Next, let’s setup the Guzzle request to submit this to the Sonar API. I’m going to embed my token right into the code as the variable $token, build up my headers, and submit the query. Even though the query is not valid JSON, we want to submit this as JSON and just treat it as a string. See below.
$token = "YOUR_TOKEN_HERE";
$client = new GuzzleHttp\Client;
$response = $client->post('https://example.sonar.software/api/graphql', [
'headers' => [
'Authorization' => "Bearer $token",
'Accept' => 'application/json',
],
'json' => [
'query' => $query
]
]);
We’re submitting an HTTP POST to /api/graphql at your Sonar URL. The two headers to include are an Authorization header where we send the token we created earlier with the prefix Bearer, and we tell the server we want to accept JSON as a response. We then submit JSON to the server as a simple object, where the property is query and the value is our query. Let’s see what we get back by decoding the response and just printing it out to the screen.
$decodedResponse = json_decode($response->getBody()->getContents(), false, JSON_PRETTY_PRINT);
print_r($decodedResponse);

As you can see, we get this back in the same format I showed previously using GraphiQL. The top level property is data, followed by the query name of accounts, and then the fields we requested.
Next, let’s submit some inputs here — rather than just getting all accounts, I’d like to submit a search object to get back accounts named Peter, like I did in an earlier article with Raphael.
First, let’s build up the search object in the same manner as we did previously. This time, we’re just going to build it as an array in PHP. Here’s the example from the last article:


To build this up in PHP, we’ll do the following.
$variables = [
'search' => [
'string_fields' => [
[
'attribute' => 'name',
'search_value' => 'Peter',
'match' => true,
'partial_matching' => true
]
]
]
];
Next, we need to add this to our Guzzle request. To submit variables, we just add another property into the body named variables.
$client = new GuzzleHttp\Client;
$response = $client->post('https://example.sonar.software/api/graphql', [
'headers' => [
'Authorization' => "Bearer $token",
'Accept' => 'application/json',
],
'json' => [
'query' => $query,
'variables' => $variables
]
]);
Lastly, we need to update the query to make use of the variables. Note that I’m escaping the $ character here, as if we don’t, PHP will interpret it as a variable reference. We could also convert to using nowdoc syntax instead.
$query = <<<GQL
query MyCoolQuery(\$search: Search) {
accounts(search: [\$search]) {
entities {
id
name
created_at
}
}
}
GQL;
Finally, let’s run our script again by executing php medium.php.

As you can see, our search object was applied, and we only got back accounts that have Peter in the name property.
Hopefully this is enough to get you started! Share what you build — I’d love to hear about it.