In most cases, you configure contexts at design-time (when you are building an agent). For example, when you have a training phrase like: "I would like to add pizza to my shopping cart."
In some advanced scenarios, you may also want to write code that gets and sets some contexts at runtime (during a live conversation). An example could be, you detected a device location and you have added it to the context, so you can refer to the location at a later moment.
This document describes how to get and set contexts at design-time and runtime by using the Dialogflow Console , the API , or fulfillment .
Context naming
The following rules apply to naming contexts:
| Rule | Examples | 
|---|---|
| Use alphanumeric names. | mycontext1 | 
| Use -or_instead of spaces. | my-context-1 | 
| Names are not case sensitive. | Abc123andabc123are considered equivalent. | 
| All context names are lowercase when using the API. | abc123 | 
Set contexts at design-time
Console
- Go to the Dialogflow ES console .
- Select an agent.
- Select Intentsin the left sidebar menu.
- Expand the Contextssection of the intent data.
- Click the Add output contextor Add input contextfield in intent data.
- Enter a name for the context and press Return.
- For output contexts, optionally click the lifespan circle and change the lifespan. 
- Add more contexts as needed.
- Click Save.
API
See the Intents reference .
Set contexts at runtime
Fulfillment
When your webhook service sends a  WebhookResponse 
 
,
set the outputContexts 
field to desired active contexts.
API
The request for a  Sessions 
 
type detectIntent 
call contains a queryParameters.contexts 
field,
which is used to set active contexts.
The following is a REST JSON example of a detectIntent 
request:
 POST https://dialogflow.googleapis.com/v2/{session=projects/*/agent/sessions/*}:detectIntent 
{ "queryInput": { "text": { "languageCode": "en-US", "text": "I would like to add pizza to my shopping cart." } }, "queryParams": { "contexts": [{ "name": "projects/ project-id /agent/sessions/ session-id /contexts/product-chosen", "lifespanCount": 5, "parameters": { "product": "Pizza", "device-location" "@52.3377871,4.8698096,17z" } }] } }
At any time,
you can also call the create, delete, and update methods on the  Contexts 
 
type.
Get contexts at runtime
Fulfillment
When your webhook service receives a  WebhookRequest 
 
,
the queryResult.outputContexts 
field contains the active contexts.
API
The response to a  Sessions 
 
type detectIntent 
call contains a queryResult.outputContexts 
field,
which provides the list of active contexts.
The following is a REST JSON example of a detectIntent 
response:
{ "responseId": " response-id ", "queryResult": { "queryText": "I would like to add pizza to my shopping cart.", "parameters": { "product": "pizza" }, "allRequiredParamsPresent": true, "fulfillmentText": "The product has been added.", "fulfillmentMessages": [ { "text": { "text": [ "The product has been added." ] } } ], "outputContexts": [ { "name": "projects/ project-id /agent/sessions/ session-id /contexts/product-chosen", "lifespanCount": 5, "parameters": { "product": "Pizza", "device-location" "@52.3377871,4.8698096,17z" } } ], "intent": { "name": "projects/ project-id /agent/intents/ intent-id ", "displayName": "buy-product" }, "intentDetectionConfidence": 0.8057143, "languageCode": "en", } }
At any time,
you can also call the get and list methods on the  Contexts 
 
type.

