Fastly Build Status

Client library for interacting with the Fastly web acceleration service API


Add fastly to your Gemfile:

gem 'fastly'

Create a fastly client:

# some_file.rb
# use {api_key: 'your-key'} or {user: 'username', password: 'password'} as login options
fastly =

current_user     = fastly.current_user
current_customer = fastly.current_customer

user     = fastly.get_user(
customer = fastly.get_customer(

puts "Name: #{}"
puts "Works for #{}"
puts "Which is the same as #{}"
puts "Which has the owner #{}"

List the services we have defined:

fastly.list_services.each do |service|
  puts "Service ID: #{}"
  puts "Service Name: #{}"
  puts "Service Versions:"
  service.versions.each do |version|
    puts "\t#{version.number}"

service        = fastly.create_service(name: "MyFirstService")
latest_version = service.version

Create a domain and a backend for the service:

domain =
                       version: latest_version.number,
                       name: "")

backend =
                        version: latest_version.number,
                        name: "Backend 1",
                        ipv4: "",
                        port: 80)

Activate the service:


You're now hosted on Fastly.

Let's look at the VCL that Fastly generated for us:

vcl = latest_version.generated_vcl

puts "Generated VCL file is:"
puts vcl.content

Now let's create a new version:

new_version = latest_version.clone

Add a new backend:

new_backend =
                        version: new_version.number,
                        name: "Backend 2",
                        ipv4: "",
                        port: 8080)

Add a director to switch between them:

director =
                         version: new_version.number,
                         name: "My Director")


Upload some custom VCL (presuming we have permissions):

custom_vcl =

new_version.upload_vcl(vcl_name, custom_vcl)

Set the custom VCL as the service's main VCL



Efficient purging

Purging requires your Fastly credentials and the service you want to purge content from. To purge efficiently you do not want to look up the service every time you issue a purge:

fastly  = 'YOUR_API_KEY')
service ={ id: 'YOUR_SERVICE_ID' }, fastly)

# purge an individual url

# purge everything:

# purge by key:

# 'soft' purging
# see
fastly.purge(url, true)
service.purge_by_key('YOUR_SURROGATE_KEY', true)

You can also purge without involving the Fastly client by sending a PURGE request directly to the URL you want to purge. You can also send a POST request to the API with your Fastly API key in a Fastly-Key header:


curl -H 'Fastly-Key: YOUR_API_KEY' -X POST \

Previously purging made an POST call to the /purge endpoint of the Fastly API.

The new method of purging is done by making an HTTP request against the URL using the PURGE HTTP method.

This gem now uses the new method. The old method can be used by passing the use_old_purge_method option into the constructor.

fastly = true))
fastly.purge(url, true)
service.purge_by_key('YOUR_SURROGATE_KEY', true)

See the Fastly purging API documentation for more information and examples.

Usage notes

If you are performing many purges per second we recommend you use the API directly with an HTTP client of your choice. See Efficient Purging above.

fastly-ruby has not been audited for thread-safety. If you are performing actions that require multiple threads (such as performing many purges) we recommend you use the API directly.


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Notes for testing

The test suite requires the following ENV variables to be set:

  • FASTLY_TEST_USER - Your user email
  • FASTLY_TEST_PASSWORD - Your account password
  • FASTLY_TEST_API_KEY - Your API key (found at

While the test suite is safe to be run on all accounts and isn't harmful to your data, the tests will create and delete 3 services in sequence so you may want to create an account just for tests.

Copyright 2011-2014 - Fastly Inc


MIT license, see LICENSE.


Mail support at fastly dot com if you have problems.