Mar 07

Accessing the web using Lua

As a follow-up to a previous post: "Getting started with Lua" I will go into a bit of how to use Lua to access web pages and retreive information over the web. In particular this post wil be about using Lua, Lua socket (for web access) and LuaRocks (for managing Lua modules) to access the Twitter api.

So there are a couple of tools I used to access the web from Lua that I wanna go through first.

  1. LuaRocks
  2. LuaRocks is defined as a tool that allows you to install Lua modules as self-contained packages called "rocks", which also contain version dependency information (see: After installation it's as easy as luarocks install [name of luarock]. There are more functions that will be listed after just typing luarocks on the command line.

  3. LuaSocket
  4. According to the website "LuaSocket is a Lua extension library that is composed by two parts: a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for functionality commonly needed by applications that deal with the Internet." Sound perfect for the use case of accessing the web from a Lua script.

  5. Twitter API
  6. Twitter is a famous tool to exchange 140 characters long messages. I offers a nice API that will be useful for testing the web access. In particular the paragrph about using A command line is all you need to use the Twitter API is of interest.

Use LuaRocks to install LuaSocket

LuaRocks makes this step really easy. All you need to do is type

luarocks install luasocket

on the command line to install the LuaSocket module to your system. To test if everything worked, use the lua shell and type

-- Use the luarocks module to load from the 'rocks' module
-- Load the specific rock: 'socket'

If everything is correct this should run without an error.

Use LuaSocket to access the Twitter API

Now that LuaSocket is installed properly we can use it to access the web. For that we first have to load the required modules:

-- use luarocks to load the modules
-- load the http module
local io = require("io")
local http = require("socket.http")
local ltn12 = require("ltn12")

This loads the required modules and binds the module to the http namespace which makes it easier to use later.

As for every access to a web page you need an URL, we need to construct one. The URL is a simple string that will be handed over to the http module. We just have to get the user login information in order to access the timeline of the user's friends:

print("Enter username: ")
username =
print("Enter password: ")
password =
authentication = username .. ":" .. password
myurl = "http://" .. authentication .. ""

All there is left to do now is to hand over the URL to the http module to perform the request:

url = myurl,
sink = ltn12.sink.file(io.stdout)

The sink parameter will write the result of the request to a lnt12 compatible target (in our case this is the standard output).

This should give you the output of your timeline in form of a XML file. There are more formats available, such as JSON or RSS. I commited the lua file to my Git repository. I hope this will help others to get started using Lua on the web and / or using the Twitter API. There are a lot of chances in both of them. My plan is to use this tiny lua script and extend it to use it as a "full" twitter client ;-) ... Well a command line twitter client using Lua. Next step is to send a statud update from lua to twitter as so far this is only a request to retreive data. I also need to get into more depth using the LuaSocket module. Stay tuned if you are interested and please do leave a comment and / or feedback. Thanks!