Lua wrapper for talking to the github api
Lua Makefile
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


lua-github is a small wrapper around the github API. It's currently primarily focused on user, org and team read-operations but the hope is to extend it to as much of the github api as possible.


Easiest option is probably to install from luarocks:

luarocks install github

Alternately, you can install via the included Makefile. You'll probably want to override the LUA_SHAREDIR environment variable. You'll also need to make sure you install my httpclient library. Since github uses SSL, if you're using the luasocket driver you'll need luasec as well. If you're only using it via ngx-lua/openresty, you shouldn't need luasec.


The following versions were tested

  • lua 5.2 (the version that shipped with trusty)
  • httpclient 0.1.0-6
  • openresty


Below are a few examples of usage both with nginx and luasocket/luasec. Results of most api calls return either

  • a lua table of the json (via cjson.decode) and nil error or
  • a nil result and an error message

Some api calls return true/false and any relevant error.

straight lua

local inspect = require 'inspect'
local gh = require('github').new()
-- alternately: local gh = require('github').new({access_token = 'XXXXXXXXX'})
local r, err = gh:get_authenticated_user()
-- no access token

-- alternately: local gh = require('github').new({access_token = 'XXXXXXXXX'})

local r, err = gh:get_authenticated_user()

if err then
-- {
--   avatar_url = "",
--   bio = <userdata 1>,
--   blog = "",
--   company = "The Lusis Group",
--   created_at = "2010-03-23T20:28:44Z",
--   email = "",
--   events_url = "{/privacy}",
--   followers = 231,
--   followers_url = "",
--   following = 97,
--   following_url = "{/other_user}",
--   gists_url = "{/gist_id}",
--   gravatar_id = "",
--   hireable = false,
--   html_url = "",
--   id = 228958,
--   location = "Roswell, GA.",
--   login = "lusis",
--   name = "John E. Vincent",
--   organizations_url = "",
--   public_gists = 231,
--   public_repos = 137,
--   received_events_url = "",
--   repos_url = "",
--   site_admin = false,
--   starred_url = "{/owner}{/repo}",
--   subscriptions_url = "",
--   type = "User",
--   updated_at = "2014-12-03T20:56:53Z",
--   url = ""
-- }

-- lusis
local r, err = gh:get_authed_user_org_membership('github', 'lusis')
-- false
local r, err = gh:get_authed_user_org_membership('logstash', 'lusis')
-- true

nginx usage

The nginx usage is largely similar but you'll need to follow the httpclient instructions to set up the internal redirect in nginx. Then you'll use this library specifying the alternate driver:

ngx.var.access_token = 'XXXXXXX'
local gh = require('github').new({access_token = ngx.var.access_token, httpclient_driver = 'httpclient.ngx_driver'})
local r, err = gh:get_authenticated_user()
if err or r.login ~= 'lusis' then
  ngx.say("welcome "..r.login)

oauth usage

Alternately you can make all calls with oauth credentials but it's up to you to store those somewhere inside nginx each authenticated user. The library provides a few helpers you can use:

  • Generate a redirect url for oauth requests: gh:get_authorize_url(client_id, scope)
  • Process an oauth callback code to generate a usable auth token: gh:request_token(callback_code, args)

In the second example, args is a lua table like so:

args = {
  client_id = 'XXXXXXX',
  client_secret = 'YYYYYYY',
  redirect_uri = 'http://hostname/my_oauth_callback'

Authenticated vs Unauthenticated calls

Note that the library can be used out of the box without having a github access token. Most calls provide authenticated vs unauthenticated versions. For example:

  • get_user(username) vs
  • get_authenticated_user()

The first option makes a call to<username> while the second makes a call to This allows you to use public calls for some operations where the token isn't neccessary.

Debugging the underlying httpclient

A feature of httpclient is the ability to get details about the last request made. The github library exposes the raw httpclient via gh.hc. Any functions available to straight httpclient can be used here as well like res = gh.hc:get('').

With every action in httpclient you can always call get_last_request() which will return some information about the driver as well as the data passed in to driver to make the request. For example while writing this library, I ran into some header related bugs with nginx. I was able to debug this information like so:

local inspect = require 'inspect'
ngx.log(ngx.ERR, inspect(gh.hc:get_last_request().get_headers()))

Which is where I realized that my accept header wasn't being passed to the github api properly.


  • tests
  • environment variable + bin script for a quick cli client
  • more docs
  • docker/rocket for quick openresty environment for testing