# Programmatic API

If you need more control over when and how tags are loaded, the Didomi SDK exposes a function to get an observable on the consent status for a given vendor. This allows you to listen to changes to the consent status and react to them to decide when to enable a vendor.

Here are a few use cases and how to implement them with the [getObservableOnUserConsentStatusForVendor](/cmp/web-sdk/reference.md#getobservableonuserconsentstatusforvendor-vendorid) function. You can find more examples and documentation on the function in the [Reference](/cmp/web-sdk/reference.md#getobservableonuserconsentstatusforvendor-vendorid).

## Enable a vendor when the user has allowed it

With this structure, your function gets called exactly once and only when the user has given consent to the vendor specifically. It could be immediately if the user has already given consent or later on after the user gives consent.

```javascript
window.didomiOnReady = window.didomiOnReady || [];
window.didomiOnReady.push(function (Didomi) {
    if (Didomi.isConsentRequired()) {
        // Consent is required: your visitor is from the EU or you are an EU company
        // Only enable the vendor when consent is given
        Didomi.getObservableOnUserConsentStatusForVendor('vendor-id')
            .filter(function(status) { return status === true; }) // Filter out updates where status is not true
            .first() // Only get the first consent status update
            .subscribe(function (consentStatusForVendor) {
                // The user has given consent to the vendor
                // Enable it
            });
    } else {
        // Consent is not required, enable your vendor immediately
    }
});
```

{% hint style="info" %}
If your tag is configured to only collect consent for visitors from the EU, you can enable all your tags for other visitors without waiting for the consent. Use the [isConsentRequired()](/cmp/web-sdk/reference.md#isconsentrequired) function to check if consent is required or not for the current visitor on the page.
{% endhint %}

## Get notified when the consent status for a vendor changes

With this structure, your function gets called every single time the consent status for the vendor changes. If the user has not given consent information yet, your function is called immediately with `undefined` as the consent status. If the consent status of the user changes multiple times on the page, the function will be called multiple times.

```javascript
window.didomiOnReady = window.didomiOnReady || [];
window.didomiOnReady.push(function (Didomi) { 
    if (Didomi.isConsentRequired()) {
        // Consent is required: your visitor is from the EU or you are an EU company
        // Only enable the vendor when consent is given
        Didomi.getObservableOnUserConsentStatusForVendor('vendor-id')
            .subscribe(function (consentStatusForVendor) {
                // The consent status for the vendor has changed
                if (consentStatusForVendor === true) {
                    // The vendor has consent
                } else if (consentStatusForVendor === false) {
                    // The vendor does not have consent
                } else if (consentStatusForVendor === undefined) {
                    // The consent status for the vendor is unknown
                }
            });
    } else {
        // Consent is not required, enable your vendor immediately
    }
});
```

{% hint style="info" %}
If your tag is configured to only collect consent for visitors from the EU, you can enable all your tags for other visitors without waiting for the consent. Use the [isConsentRequired()](/cmp/web-sdk/reference.md#isconsentrequired) function to check if consent is required or not for the current visitor on the page.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.didomi.io/cmp/web-sdk/third-parties/programmatic-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
