

# Usar pares de chave-valor em uma solicitação do visualizador do CloudFront Functions
<a name="example_cloudfront_functions_kvs_key_value_pairs_section"></a>

O exemplo de código a seguir mostra como usar pares de chave-valor em uma solicitação do visualizador do CloudFront Functions.

------
#### [ JavaScript ]

**Runtime 2.0 do JavaScript para o CloudFront Functions**  
 Há mais no GitHub. Encontre o exemplo completo e saiba como configurar e executar no [repositório de exemplos do CloudFront Functions](https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/kvs-key-value-pairs). 

```
import cf from 'cloudfront';

// This fails if there is no key value store associated with the function
const kvsHandle = cf.kvs();

// Remember to associate the KVS with your function before referencing KVS in your code.
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions-associate.html
async function handler(event) {
    const request = event.request;
    // Use the first segment of the pathname as key
    // For example http(s)://domain/<key>/something/else
    const pathSegments = request.uri.split('/')
    const key = pathSegments[1]
    try {
        // Replace the first path of the pathname with the value of the key
        // For example http(s)://domain/<value>/something/else
        pathSegments[1] = await kvsHandle.get(key);
        const newUri = pathSegments.join('/');
        console.log(`${request.uri} -> ${newUri}`)
        request.uri = newUri;
    } catch (err) {
        // No change to the pathname if the key is not found
        console.log(`${request.uri} | ${err}`);
    }
    return request;
}
```

------

Para ver uma lista completa dos guias de desenvolvedor e exemplos de código do SDK da AWS, consulte [Como usar o CloudFront com um SDK da AWS](sdk-general-information-section.md). Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.