Our code examples library is an always-evolving set of samples to do just about anything on the Fastly edge cloud. Browse or search for a keyword to find the perfect, ready-made solution you can paste into your service or adapt and customize for your own needs. Using Fastly Fiddle, play with any example and take advantage of the Fastly edge cloud, all without registering or affecting any existing account.
Detect requests that don't include a www. prefix, and redirect to the equivalent path on a hostname that starts with www., usually to make sure there's only one canonical location for your content.
Normally OPTIONS requests aren't cacheable. Allow caching of OPTIONS by converting the request to a GET and back to OPTIONS if it's a cache miss.
Fastly imposes per-request resource limits on customers' use of our edge network. If you set too many headers, this happens.
Create an identifier that looks like an IPv4 address if the user has made their request using IPv6. Useful for origin servers that require an IPv4 address for some reason.
Go from an F to an A grade on securityheaders.io by adding security policy headers to your responses at the edge.
Detect situations when a shield server might have a different version of your code than the edge server, and avoid errors that this might cause.
Receive a request for one path but request a different path from origin, without a redirect.
Change PUT, DELETE, OPTIONS and others to POST, or vice versa, to help integrate incompatible client and server apps.
Use addition, subtraction, multiplication, division, and modulus operators when assigning numeric values to headers or variables.
Treat URLs with and without suffixed slashes as equivalent, or redirect URLs with slashes to the version without.
Use our re.group variable to capture regex pattern groups.
Use regular expression substitution functions (regsub) to map paths, strip extraneous slashes, and more.
Logical expressions involving the left side of an assignment as an operand.
Isolate a portion of a string identified by a range of characters.
To allow caching of POST requests, consider rewriting them as GET requests at the edge.
Cached a large number of objects for too long and want to update and shorten their TTLs.
Identify which type of IP address was used by the client connecting to your Fastly service.
GraphQL query requests are POSTs, but normally responses to POST can't be cached. Normalize the query and convert it to a querystring on a GET request to allow Fastly to cache these responses.
Divide the world into time bands of custom size and forward time zone data to your origin server.
Format dates and times in a variety of ways.
Adjust the maximum TCP socket pacing for connections at peak times of day in busy regions.
Use AWS compat mode to make authenticated requests to your GCS bucket.
Validate your CenturyLink tokens for access to video stream playlists.
All code on this page is provided under both the BSD and MIT open source licenses.