---
title: table.lookup
summary: null
url: >-
  https://www.fastly.com/documentation/reference/vcl/functions/table/table-lookup
---

```
STRING table.lookup(ID id, STRING key, STRING default?)
```

**Available in:** all subroutines

Looks up the key `key` in the table `ID`. When the key is present, its
associated `STRING` value will be returned.
A _not set_ value is returned if the key is not present in the table.

This works whether a table contains literal keys, regex keys,
or a mix of both.

Either short- or long-form strings are supported for keys and for values
using [STRING literals](https://www.fastly.com/documentation/reference/vcl/types/string).

You can use `table.contains()` to check for the presence of a key.
If you plan to do a lookup of that key's value, consider combining
the calls into a single lookup to find the key's presence and the
stored value at the same time. For example:

```vcl
# note this depends on using a header, not a STRING variable
set req.http.x = table.lookup(t, "key");
if (!req.http.x) { # equivalent to if (!table.contains(t, "key"))
  # no such key
} else {
  # use the value of req.http.x
}
```

This works because a _not set_ header compares false in conditions.
The condition above depends on using a header for the returned value
and not a local STRING variable. This is because a _not set_ value
is converted to an empty string when assigned to a STRING variable
and the empty string always compares true in conditions.
See [VCL Types](https://www.fastly.com/documentation/reference/vcl/types/string/)
for details of the type conversion.

When a third `STRING` argument is provided, the lookup function behaves as
it would normally, except when a key is absent, the `default` value is
returned instead.

You can also use `table.lookup_interpolate`,
which allows using parts of the matching key in a `STRING` value.
This is especially useful for tables with regex keys using group capture,
but also works for literal keys.

## Examples

```vcl
table geoip_lang {
  "US": "en-US",
  "FR": "fr-FR",
  "NL": "nl-NL",
}

if (!req.http.Accept-Language) {
  set req.http.Accept-Language = table.lookup(geoip_lang, client.geo.country_code, "en-US");
}
```

Here's an example using `table.lookup` on regex keys with no group capture.
See `table.lookup_interpolate` for examples using group capture instead.

```vcl
table extension {
  ~ "xhtml5?": "xhtml",
  ~ "htm(?:l5?)?": "html", # (?:...) is a non-capturing group
  ~ "aiff?": "aiff",
  ~ "tiff?": "tiff",
  ~ "jpe?g": "jpeg",
  ~ "mpe?g": "mpeg",

    # you can mix literal keys and regex keys in the same table
    "stp": "step",
    "svg": "xml",
}

sub vcl_recv {
  # normalizing URL file extensions
  declare local var.ext STRING;

  # lowercase for both the table key and for the default value
  set var.ext = std.tolower(req.url.ext);
  set var.ext = table.lookup(extension, var.ext, var.ext);
}
```
