NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • Application Programming Interface (API)
  • HTTP Requests
  • Let's Start
  • Piecing it all together
  • Another Example
Edit on GitHub
Export as PDF
  1. Orbital
  2. Telegram Bot

API Calls

PreviousCreating a TeleBotNextTelebot Basics

Last updated 12 months ago

Application Programming Interface (API)

It's a "contract" that defines how separate software interacts with each other.

HTTP Requests

GET - get data (what are the available timings tomorrow)

POST - creating or inserting new data (making a lunch reservation)

PUT - update existing data (updating the timing for a lunch reservation)

DELETE - deletes data (removing your lunch reservation)

Let's Start

First, we import an external library that helps us make API calls

import requests

Next, we will query an API of your choice.

  • Google the API you are looking for (e.g. searching "random cat facts API" )

  • We will then make use of this API:

To obtain a random fact, we call .get on /fact

# add /fact to the end of the API url
endpoint = "https://catfact.ninja/fact"


# Sends a GET request to the endpoint
# This calls a random fact 
response = requests.get(endpoint)
response
  • When running this, you should get <Response [200]> .

  • A status code of 200 means the API call was successful.

Let's call the data we received

response.text

We should get a random fact about cats in a dictionary

# '{"fact":"Some common houseplants poisonous to cats include: English Ivy, iris, mistletoe, philodendron, and yew.","length":103}'

API responses are typically encoded as JSON (JavaScript Object Notation). For us to use it in python, we can call the .json() method to convert it into a dictionary.

response.json()
# {"fact":"Some common houseplants poisonous to cats include: English Ivy, iris, mistletoe, philodendron, and yew.","length":103}

# type: dict

Call the fact and print the String

fact = response.json()["fact"]
print(fact)
# Some common houseplants poisonous to cats include: English Ivy, iris, mistletoe, philodendron, and yew.

Piecing it all together

def get_cat_fact():
    endpoint = "https://catfact.ninja/fact"
    response = requests.get(endpoint)
    fact = response.json()["fact"]
    return fact

print(get_cat_fact())

Another Example

Finding the parking lots available at a carpark

endpoint = "https://api.data.gov.sg/v1/transport/carpark-availability"

response = requests.get(endpoint)

This sends a query to the API to obtain the carpark availability

# Need to figure out how to navigate the data to extract only what is necessary

carpark_data = response.json()["items"][0]["carpark_data"]
len(carpark_data)

Retreives the number number of carparks with data

for carpark_info in all_carpark_info:
    if carpark_info["carpark_number"] == "HE12":
        print(carpark_info)
        break
# pulls out the respective information of capark HE12

Description of the API can be found

here
https://catfact.ninja/
The Cat Facts API