Getting the Current Fastly VCL via API

Published: September 28, 2022

Tags:

This is just a quick little tip, but something I always have to look up how to do. The Fastly API has an endpoint for fetching the generated VCL for a service. The issue is that the version_id for the serivce must be provided in the request (there’s no way to say “just give me the currently active VCL”).

As such, we need to first use the List versions of a service endpoint to identify the active version. From there we can request the generated VCL.

Putting this all together, assuming you have jq installed, here’s how to do this in practice (replace FASTLY_KEY and SERVICE_ID with the correct values for your project).

For Magento Cloud environments, the Fastly Key and Service ID can be found in the /mnt/shared/fastly_tokens.txt file. "API Token" is the FASTLY_KEY and "Serivce ID" is the SERVICE_ID.

# Get the active version. In this example 105 is active
$ curl --silent -H "Fastly-Key: FASTLY_KEY" https://api.fastly.com/service/SERVICE_ID/version \
  | jq '.[] | if .active then .number else empty end'
105

# Get the VCL
$ curl -H "Fastly-Key: FASTLY_KEY" https://api.fastly.com/service/SERVICE_ID/version/105/generated_vcl
{"version":105,"service_id":"...

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.