Network and internet

Call an API from the terminal

Ask a web service for data with one command and see the answer straight away.

Fill these in once
  1. Ask the GitHub API about a user
    Terminal
    curl -s https://api.github.com/users/octocat

    The answer comes back as JSON: labelled data about that account.

Check it worked

  1. Send your API key with the request
    Terminal
    curl -s -H "Authorization: Bearer $API_KEY" https://api.github.com/user

    Put your key in the API_KEY environment variable first, never in the command itself.

  1. Ask the GitHub API about a user
    Terminal · Ubuntu / Debian
    curl -s https://api.github.com/users/octocat

    The answer comes back as JSON: labelled data about that account.

Check it worked

  1. Send your API key with the request
    Terminal · Ubuntu / Debian
    curl -s -H "Authorization: Bearer $API_KEY" https://api.github.com/user

    Put your key in the API_KEY environment variable first, never in the command itself.

  1. Ask the GitHub API about a user
    PowerShell
    Invoke-RestMethod -Uri https://api.github.com/users/octocat

    PowerShell turns the JSON answer into a tidy list.

Check it worked

  1. Send your API key with the request
    PowerShell
    Invoke-RestMethod -Uri https://api.github.com/user -Headers @{ Authorization = "Bearer $env:API_KEY" }

    Put your key in the API_KEY environment variable first, never in the command itself.

New to API? I explain it in plain English on Layman Terms.