---
title: "Formula - contextKey: Return Matching Message Context Key Names"
description: The contextKey Formula function returns the key names of the Message Context properties whose key matches a regular expression. Includes syntax, parameters, and an order example.
---

# <i class="fal fa-function" aria-hidden="true"></i> Formula - ContextKey

The `contextKey` [Formula][] function returns the key names (not the values) of the [Message Context properties][JSON Log Event - Context] of the [Log Event][] whose key matches a regular expression.

![List the customer context keys of an order with contextKey, then count them, animated walkthrough (7.12)][2]  
*Animated walkthrough: on the Message Context of the Order Received event of order ORD-10005 in the [Order-to-Cash demo][], `contextKey(regex('^Customer'))` returns the key names `CustomerId` and `CustomerName`, not their values. Nested in [count][], the same call returns `2`.*  

## <i class="fal fa-code" aria-hidden="true"></i> Syntax

```text
contextKey(regex('Key pattern'))
```

### Parameters

|Parameter|Required|Description|  
|---|---|---|  
|`regex('Key pattern')`|Yes|A [regex][] that is matched against the context key names.|  

### Returns

Every matching key name, one value per key. Duplicates are collapsed (see [Formula language rules][]). When no key matches, nothing is returned. To get the values of the matching context keys instead, use [context][Context] with the same `regex(...)` argument.

## <i class="fal fa-list-ol" aria-hidden="true"></i> Examples

### Example 1: List the order keys in the context

#### Input

|Key|Value|  
|---|---|  
|Order#123|OK|  
|Order#456|OK|  
|Order#789|Fail|  

#### Formula

```text
contextKey(regex('Order#.*'))
```

#### Result

```text
Order#123
Order#456
Order#789
```

![Formula contextKey expression returning three Order key names from the Message Context tab (7.12)][1]  
*Each key that matches `Order#.*` is returned as its own unique match.*  

## <i class="fal fa-circle-info" aria-hidden="true"></i> Notes

* `contextKey` returns the whole key name whenever the pattern matches part of it. The pattern matches anywhere in the key name unless you anchor it: use `^Order[#]` to match only keys that start with `Order#`.
* In Formula regular expressions `#` starts a comment, so `'Order#.*'` in the example really runs as `'Order'`. It works here because every key starts with `Order`; write `[#]` or `\#` when the `#` matters. See [Formula language rules][].
* Combine with [count][] to get the number of matching keys, for example `count(contextKey(regex('Order#.*')))`.

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

* <i class="fal fa-magnifying-glass-plus" aria-hidden="true"></i> [Add or manage Search Field][]  
* <i class="fal fa-flask" aria-hidden="true"></i> [Test Your Expression][]  
* <i class="fal fa-hard-drive" aria-hidden="true"></i> [Add or manage Log View][]  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

* <i class="fal fa-envelope" aria-hidden="true"></i> [body][]  
* <i class="fal fa-key" aria-hidden="true"></i> [context][Context]  
* <i class="fal fa-table-list" aria-hidden="true"></i> [field][]  
* <i class="fal fa-function" aria-hidden="true"></i> [Formula][]  
* <i class="fal fa-scroll" aria-hidden="true"></i> [Formula language rules][]  
* <i class="fal fa-puzzle-piece" aria-hidden="true"></i> [Search Field Expressions][]  
* <i class="fal fa-envelope" aria-hidden="true"></i> [Log Event][]  
* <i class="fal fa-magnifying-glass" aria-hidden="true"></i> [Message Context Key plugin][]  
* <i class="fal fa-key" aria-hidden="true"></i> [Context Properties tab in a Log View][Context Properties tab]  

[1]:https://docs.nodinite.com/media/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/ContextKey.png
[2]:https://docs.nodinite.com/media/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/Formula_ContextKey.gif

[Add or manage Log View]:https://docs.nodinite.com/administration/log-management/log-views/features/add-or-manage-log-view/
[Add or manage Search Field]:https://docs.nodinite.com/administration/log-management/search-fields/features/add-or-manage-search-field/
[body]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/source/body/
[context]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/source/context/
[Context Properties tab]:https://docs.nodinite.com/getting-started/log/see-the-details-of-a-log-event/#context-properties
[count]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/math/count/
[field]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/source/field/
[Formula]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/overview/
[Formula language rules]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/overview/#fa-scroll-formula-language-rules
[JSON Log Event - Context]:https://docs.nodinite.com/core-services/log-event/features/json-log-event/#context
[Log Event]:https://docs.nodinite.com/core-services/log-event/overview/
[Message Context Key plugin]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/message-context-key/
[Order-to-Cash demo]:https://docs.nodinite.com/repository-model/bpms/examples/order-to-cash/generate-o2c-demo-data/
[regex]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/regex/
[Search Field Expressions]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/overview/
[Test Your Expression]:https://docs.nodinite.com/administration/log-management/search-fields/features/add-or-manage-search-field/#test-your-expression