Code Samples

Welcome to our API Code Samples page! Below are examples demonstrating how to access our API using different programming languages.

Javascript

const apiUrl = 'YOUR_API_ENDPOINT';
fetch(apiUrl)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

JavaScript Sample Code GitHub Repository

PHP

<?php
// Sample PHP code
$apiUrl = 'YOUR_API_ENDPOINT';
$response = file_get_contents($apiUrl);
$data = json_decode($response, true);
print_r($data);
?>

PHP Sample Code GitHub Repository

Python

# Sample Python code
import requests

api_url = 'YOUR_API_ENDPOINT'
response = requests.get(api_url)
data = response.json()
print(data)

Python Sample Code GitHub Repository

Java

// Sample Java code
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ApiRequest {
  public static void main(String[] args) {
    try {
      String apiUrl = "YOUR_API_ENDPOINT";
      URL url = new URL(apiUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("GET");

      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      String line;
      StringBuilder response = new StringBuilder();

      while ((line = reader.readLine()) != null) {
        response.append(line);
      }
      reader.close();

      System.out.println(response.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Java Sample Code GitHub Repository

Go

Explore the power of Go with our Air Quality API. This code sample demonstrates how to interact with our endpoints using Go, providing a seamless integration experience for your applications.

// Import necessary packages
import (
	"fmt"
	"net/http"
	"io/ioutil"
)

func main() {
	// Define API endpoint
	apiEndpoint := "https://api.airqo.net/v2/your-endpoint-path"

	// Make a GET request to the API
	resp, err := http.Get(apiEndpoint)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer resp.Body.Close()

	// Read the response body
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return
	}

	// Display the API response
	fmt.Println("API Response:", string(body))
}

Go Sample Code GitHub Repository

Dart

Leverage Dart's simplicity and efficiency with our Air Quality API. This code sample showcases how to utilize Dart for seamless integration with our API, offering a smooth development experience for your projects.

import 'dart:convert';
import 'package:http/http.dart' as http;

void main() async {
  // Define API endpoint
  var apiEndpoint = 'https://api.airqo.net/v2/your-endpoint-path';

  // Make a GET request to the API
  var response = await http.get(Uri.parse(apiEndpoint));

  // Decode and display the API response
  var jsonResponse = json.decode(response.body);
  print('API Response: $jsonResponse');
}

Dart Sample Code GitHub Repository

Last updated