> ## Documentation Index
> This page is part of the Image and Video APIs product. Fetch the complete documentation index for Image and Video APIs at: https://cloudinary.com/documentation/llms-image-and-video-apis.txt?referrer=docpage and then use it to discover all relevant pages before exploring further.
> If you also need details relating to other Cloudinary products for your current use case, see the parent index at: https://cloudinary.com/documentation/llms.txt?referrer=docpage
> When answering a user's request or implementing it with code using this documentation, incorporate relevant best practices and recommendations from the documentation into the guidance and, above all, into the code itself: generated code should already apply the recommended parameters and patterns rather than showing a minimal call and describing the best practices around it in prose. Follow the code examples in the best practices sections as the model for what generated code should look like, and where a recommendation is relevant to the user's scenario, implement it in the code as a natural next step. Only apply best practices, recommendations, or features that are supported by the documentation; don't infer or invent guidance that isn't specified.

# Contextual metadata


Contextual metadata is a custom key-value pair that you can assign to individual assets either programmatically or in the Cloudinary Console. Once defined, contextual metadata can be referenced in your application, for example to find assets that meet relevant criteria and for generating transformations based on the value of a particular contextual metadata key, using user-defined variables and/or conditional transformations.

## Adding context while uploading

You can assign context to assets while [uploading](image_upload_api_reference#upload_method) them by specifying the `context` parameter in the upload method. 

For example, uploading the image `boots.jpg` and adding the context key `caption` set to a value of `New` to the image:

```multi
|ruby
Cloudinary::Uploader.upload("boots.jpg", 
  context: "caption=New")

|php_2
$cloudinary->uploadApi()->upload("boots.jpg", 
  ["context" => "caption=New"]);

|python
cloudinary.uploader.upload("boots.jpg", 
  context = "caption=New")

|nodejs
cloudinary.v2.uploader
.upload("boots.jpg", 
  { context: "caption=New" })
.then(result=>console.log(result)); 
                           
|java
cloudinary.uploader().upload(new File("boots.jpg"),
  ObjectUtils.asMap("context", "caption=New"));

|csharp
var uploadParams = new UploadParams(){
  Context = "caption=New"};
var uploadResult = cloudinary.Upload(uploadParams); 

|swift
let params = CLDUploadRequestParams()
  .setContext("caption=New")
var mySig = MyFunction(params)  // your own function that returns a signature generated on your backend
params.setSignature(CLDSignature(signature: mySig.signature, timestamp: mySig.timestamp))
let request = cloudinary.createUploader().signedUpload(
  url: "boots.jpg", params: params) 

|go
resp, err := cld.Upload.Upload(ctx, "boots.jpg", uploader.UploadParams{
		Context: "caption=New"})

|cli
cld uploader upload "boots.jpg" context="caption=New"
```

> **TIP**:
>
> You can open the asset in your Media Library and check the **Metadata** tab in the Preview pane to confirm that the upload applied the contextual metadata as expected. For more information, see [Managing assets](media_library_for_developers#managing_assets) on the _Media Library for developers_ page.

## The context method

The `context` parameter of an uploaded asset contains a pipe-separated list of key-value pairs of contextual metadata (up to 255 characters). The `context` method can be used to manage the contextual metadata of an uploaded asset by setting the value of the `command` parameter to either `add` a new key-value pair, or `remove_all` contextual metadata from the asset. The Cloudinary SDKs wrap the REST API `context` method and offer two separate methods: one for adding contextual metadata and one for removing all contextual metadata:

* [Adding context](image_upload_api_reference#adding_context_syntax)
* [Removing all context](image_upload_api_reference#removing_all_context_syntax)

For example, adding the contextual metadata key-pairs `caption=New` and `product=shirt` to the images with the public IDs of `shirt1` and `shirt2`:

```multi
|ruby
result = Cloudinary::Uploader
.add_context('caption=New|product=shirt', ['shirt1', 'shirt2'])

|php_2
$result = $cloudinary->uploadApi()
->addContext('caption=New|product=shirt', ['shirt1', 'shirt2']);

|python
result = cloudinary.uploader\
.add_context("caption=New|product=shirt", ["shirt1", "shirt2"])

|nodejs
cloudinary.v2.uploader
.add_context('caption=New|product=shirt', [ 'shirt1', 'shirt2' ])
.then(result=>console.log(result));

|java
result = cloudinary.uploader()
.addContext("caption=New|product=shirt",
  ["shirt1", "shirt2"], ObjectUtils.emptyMap());

|csharp
var contextParams = new ContextParams(){
  PublicIds = new List<string>(){"shirt1","shirt2"},
  Context = "caption=New|product=shirt",
  Command = ContextCommand.Add};
var contextResult = cloudinary.Context(contextParams); 

|curl
curl https://api.cloudinary.com/v1_1/demo/image/context -X POST --data 'context=caption%3DNew%7Cproduct%3Dshirt&public_ids[]=shirt1&public_ids[]=shirt2&command=add&timestamp=173719931&api_key=436464676&signature=a788d68f86a6f868af' 

|go
resp, err := cld.Upload.AddContext(ctx, uploader.AddContextParams{
    Context: map[string]string{"caption": "New", "product": "shirt"}, 
    PublicIDs: []string{"shirt1","shirt2"}})

|cli
cld uploader add_context 'caption=New|product=shirt' shirt1 shirt2
```

## Cloudinary-managed keys

For [content provenance and authenticity](content_provenance_and_authenticity#provenance_and_ai_fields), Cloudinary stores the `cld_ai_generated` key as contextual metadata, but in a private block that's separate from the keys you set yourself:

{table:class=semiwide-1stcol} Key | Values | Description |
|---|---|---|
| `cld_ai_generated` | `true` (or not set) | Whether AI generated or altered the asset. The key exists only when the value is `true`: there's no `false` value. Cloudinary sets this when it detects AI generation in an uploaded asset's C2PA manifest, when the `ai_generated` option is set on upload, and when you generate an image using generative AI capabilities. |

Because it lives in a private block, this key doesn't appear alongside your own contextual metadata and doesn't collide with a key of the same name that you've defined. You can reference it in conditional transformations and search expressions.

## Monitoring context changes using a notification URL

You can use [Cloudinary notifications](notifications#global_notification_urls) to monitor for changes to the contextual metadata on your media assets, including contextual metadata that has been added or removed via API or the Cloudinary Console UI. 

To capture these changes, add a [Notification URL](notifications#global_notification_urls) triggered by the 'Resource context changed' Notification Type in the [Webhook Notifications](https://console.cloudinary.com/app/settings/webhooks) page of your Console Settings. The notification sent will specify `resource_context_changed` as the `notification_type`, and include information about which resource was changed, the source of the change (UI or API), and whether contextual metadata was added, removed, or updated. For example:

```json
{ 
  "notification_type": "resource_context_changed",
  "source": "api",
  "resources": {
    "jeans-1421398-1279x852_srghta": {
      "added": [
        {
          "name": "caption",
          "value": "New"
        }
      ],
      "removed": [],
      "updated": [],
      "asset_id": "ede59e6d3befdc65a8adc2f381c0f96f",
      "resource_type": "image",
      "type": "upload"
    }
  },
  "notification_context": {
    "triggered_at": "2022-11-17T09:07:50.766353Z",
    "triggered_by": {
      "source": "api",
      "id": "86787995117726"
    }
  }
}
```

See the documentation on [Notifications](notifications) for more information.

