Code examples in Go
These code examples have an implementation in Go. To learn more about using Go with our Compute@Edge platform, see using Go.
NOTE: Some of the code examples here are also available in languages other than Go. Being able to see how the same solution can be achieved in other languages is often useful for migrations or learning, so where a code example has multiple implementations, we show all implementations together.
Verify if a web crawler accessing your server really is Googlebot
An implementation of Google's recommended mechanism for verifying googlebot
Add a new field to a JSON response
Decorate API responses in JSON format with new fields.
Geo-IP API at the edge
Create an API endpoint for fetching GeoIP data for the requesting browser, implemented 100% at the edge. The response should show your current approximate location, but no requests to any origin servers.
Tag requests with geolocation data
Add GeoIP data about the client browser as extra headers in any requests from Fastly to your origin.
Apply feature flags from an origin API
Park request, make a different request first, use the response to annotate the real origin request (or make decisions about how to route it).
Add www. to apex hostname and subdomains
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.
Override TTLs for path prefixes
Set TTLs at the edge based on looking up a path prefix in an edge dictionary.
Use microservices to divide up a domain
Send request to different origin servers based on the URL path.
Replace origin errors with 'safe' responses
Detect specified response statuses from backends and instead serve a precomposed error page or error content generated at the edge.
Apply HTTP basic auth to private endpoints
Store username/password list in an edge dictionary, authorize user at the edge, reject requests that don't have correct credentials.
Serve robots.txt from the edge
Serve full text of robots.txt as a synthetic response to avoid requests hitting your origin.
Create self-destructing URLs for time-limited access
Make URLs expire after a configurable period.
Enable modern web security headers to all responses
Go from an F to an A grade on securityheaders.io by adding security policy headers to your responses at the edge.
Add, remove or change HTTP headers
Fastly can easily read and write HTTP headers at multiple stages of the request/response cycle.
Rewrite URL path
Receive a request for one path but request a different path from origin, without a redirect.
Filter query string parameters
Add, remove, and sort querystring parameters.
Change request method
Change PUT, DELETE, OPTIONS and others to POST, or vice versa, to help integrate incompatible client and server apps.
Add or remove cookies
Read individual cookies, set new cookies in response.
Remove trailing slashes to normalize URLs
Treat URLs with and without suffixed slashes as equivalent, or redirect URLs with slashes to the version without.
Caching responses to POST requests
By default, Fastly does not cache responses to POST requests. But you can enable this if you wish.
Regular expression capturing patterns
Capture regex pattern groups to use in matching url segments and more.
AWS S3 bucket origin (private)
Use AWS authenticated requests (signature version 4) to protect communication between your Fastly service and AWS.
Smoke test a new origin
Send a copy of your traffic to a test origin before returning a response from production.
Decompress and read gzipped responses
When you need to work on API and text responses from backends that support gzip.
Clean backend responses
Remove headers added by backends that you don't want to emit to the browser, like amz- or goog- headers.
Follow redirects at the edge
Protect clients from redirects by chasing them internally at the edge, and then return the eventual non-redirect response.
Remove querystring from static assets
Use the new Sec-Fetch-Dest header or URL patterns to identify assets that should not allow querystrings to be part of the cache key.
Auto retry a secondary backend
If primary backend fails, retry with a different backend without caching the failure or reducing cache efficiency.
Log the IP version (IPv4/IPv6)
Identify which type of IP address was used by the client connecting to your Fastly service.
Redirect old URLs at the edge
Use a dictionary of URL mappings to serve your redirects at lightning speed.
All code on this page is provided under both the BSD and MIT open source licenses.