---
updatedAt: 2025-08-28T23:52:57.000Z
agentTools:
  projectIndex: https://docs.scope3.com/v1.3/llms.txt
---

# Snowflake (version 2)

# Create Scope3 API Integration

```sql
create or replace api integration scope3_api
    api_provider = google_api_gateway
    google_audience = ''
    api_allowed_prefixes = ('https://scope3-snowflake-api-gateway-5qbjzj9o.uc.gateway.dev')
    enabled = true;
```

# Create Scope3 External Function

## Function Call

```sql
create or replace external function scope3_v2(
  impressions int,
  date date,
  inventory_id string,
  device_type string,
  channel string,
  country string,
  region string,
  creative_format string,
  payload_size int,
  duration_seconds int,
  network string,
  seller string,
  buying_method string,
  deal_id string)
    returns OBJECT
    api_integration = scope3_api
    headers = (
        'AccessClientId'='****',
        'AccessClientSecret'='****'
    )
    as 'https://scope3-snowflake-api-gateway-5qbjzj9o.uc.gateway.dev/api/v1/snowflake';
```

<Table align={[null,null,"left",null]}>
  <thead>
    <tr>
      <th>
        Field
      </th>

      <th>
        required
      </th>

      <th>
        Description
      </th>

      <th>
        Example
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        impressions
      </td>

      <td>
        :white_check_mark:
      </td>

      <td>
        Sum of impressions for these dimensions
      </td>

      <td>
        10000
      </td>
    </tr>

    <tr>
      <td>
        date
      </td>

      <td>
        :white_check_mark:
      </td>

      <td>
        Dates must be in the past (i.e. prior to today). Dates should be in the UTC timezone in one of the following formats: `YYYY-MM-DD`, `YYYYMMDD`, or `YYYY/MM/DD`
      </td>

      <td>
        2023-04-01
      </td>
    </tr>

    <tr>
      <td>
        inventory\_id
      </td>

      <td>

      </td>

      <td>
        The inventory ID field is an alternative to site.domain and app.storeId. This field can accept either a domain or app ID. When channel is not specified, we will use inventoryId to make an informed guess.
      </td>

      <td>
        nytimes.com
      </td>
    </tr>

    <tr>
      <td>
        device\_type
      </td>

      <td>

      </td>

      <td>
        The consumer’s device: `phone`, `tablet`, `pc`, or `tv`. If not provided, we’ll infer the device type from the the channel or the device field.
      </td>

      <td>
        pc
      </td>
    </tr>

    <tr>
      <td>
        channel
      </td>

      <td>

      </td>

      <td>
        * \*Options\*\*:  display-app, display-web, streaming-video, social  

        `display-web`: Display ads, including outstream video, on a website  

        `display-app`: Display ads, including outstream video, on a mobile app. Excludes streaming and social apps  

        `streaming-video`: Streaming video including CTV and YouTube. Includes in app or browser based streaming. Currently in a closed beta.  

        `social`: Includes in app or browser based social properties. Currently in closed beta
      </td>

      <td>
        display-web
      </td>
    </tr>

    <tr>
      <td>
        country
      </td>

      <td>

      </td>

      <td>
        This is the country ISO 3166-2 alpha-2 code. e.g. US
      </td>

      <td>
        US
      </td>
    </tr>

    <tr>
      <td>
        region
      </td>

      <td>

      </td>

      <td>
        The 2 or 3 letter region code - only supported for Canada, US, and Australia
      </td>

      <td>
        NY
      </td>
    </tr>

    <tr>
      <td>
        creative\_format
      </td>

      <td>

      </td>

      <td>
        * \*Options\*\*: `banner`, `video`, `text`  

        Banner uses payload size and conventional model.  

        Video uses duration and power model.\
        Text returns 0
      </td>

      <td>
        video
      </td>
    </tr>

    <tr>
      <td>
        payload\_size
      </td>

      <td>

      </td>

      <td>
        The number of bytes transferred to fully display the creative. Only used for banner and video format.
      </td>

      <td>
        3024
      </td>
    </tr>

    <tr>
      <td>
        duration\_seconds
      </td>

      <td>

      </td>

      <td>
        Time the creative plays. Only used for video format.
      </td>

      <td>
        15
      </td>
    </tr>

    <tr>
      <td>
        network
      </td>

      <td>

      </td>

      <td>
        * \*options\*\*: `fixed`, `mobile`\
          The networking connection used by the end user's device.
      </td>

      <td>
        fixed
      </td>
    </tr>

    <tr>
      <td>
        seller
      </td>

      <td>

      </td>

      <td>
        Name of the seller entity that sold the media
      </td>

      <td>
        xandr
      </td>
    </tr>

    <tr>
      <td>
        buying\_method
      </td>

      <td>

      </td>

      <td>
        * \*options\*\*: `programmatic-open`, `programmatic-pmp`, `programmatic-guaranteed`, `direct`, `direct-takeover`  

        The method used to buy the impressions
      </td>

      <td>
        programmatic-open
      </td>
    </tr>

    <tr>
      <td>
        deal\_id
      </td>

      <td>

      </td>

      <td>
        Seller deal identifier
      </td>

      <td>
        deal-1234
      </td>
    </tr>
  </tbody>
</Table>

## Response

The function returns the following object.  Each field may be missing/null depending if the data is available.

If `total_emissions` and `error` is `null` then scope3 does noes not have information for this impression.

```json
{
  "ad_selection_emissions": "<float|null>",
  "domain_coverage": "<modeled|missing|unknown>",
  "gmp_eligible": "<true|false>",
  "matched_channel": "<display-web|display-app|streaming-video|social>",
  "matched_property": "<string>",
  "media_distribution_emissions": "<float>",
  "supply_directness": "<int>",
  "total_emissions": "<float>",
  "error": "<string|null>"
}
```

<Table>
  <thead>
    <tr>
      <th>
        Field
      </th>

      <th>
        Description
      </th>

      <th>
        Example
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        ad\_selection\_emissions
      </td>

      <td>
        Ad tech including servers and cloud computing; analytics; network traffic; storage; data providers; and vendor overhead. In grams of CO₂e.
      </td>

      <td>
        300.99
      </td>
    </tr>

    <tr>
      <td>
        domain\_coverage
      </td>

      <td>
        modeled - domains/apps are in the dataset with reasonable confidence of accuracy.\
        missing - domains/apps have not been modeled yet and will automatically be queued for analysis.\
        unknown - domains/apps appear to have errors. e.g. A site that has an invalid domain name.
      </td>

      <td>
        modeled
      </td>
    </tr>

    <tr>
      <td>
        gmp\_eligible
      </td>

      <td>
        Impressions are GMP eligible if the gCO₂e ad selection emissions per impression is lower than benchmark emissions data for the associated country and channel. If benchmarks are unavailable for a specific country then worldwide benchmarks will be used. = true;

        # Cre
      </td>

      <td>
        true
      </td>
    </tr>

    <tr>
      <td>
        matched\_channel
      </td>

      <td>
        Creative Emissions in gCO2e
      </td>

      <td>
        100.33
      </td>
    </tr>

    <tr>
      <td>
        matched\_property
      </td>

      <td>
        Modeled domains are in the dataset with reasonable confidence of accuracy. Missing domains have not been modeled yet and will automatically be queued for analysis.
      </td>

      <td>
        modeled
      </td>
    </tr>

    <tr>
      <td>
        media\_distribution\_emissions
      </td>

      <td>
        Media delivery including CMS; CDNs and hosting services; publishing overhead including employee and office expenses; data transfer. In grams of CO₂e.
      </td>

      <td>
        50.2
      </td>
    </tr>

    <tr>
      <td>
        supply\_directness
      </td>

      <td>
        Maximum depth of ad selection path to the seller based on buying method n scope3_api
            api_provider = google
      </td>

      <td>
        3
      </td>
    </tr>

    <tr>
      <td>
        total\_emissions
      </td>

      <td>
        Total footprint of the campaign (sum of above) in grams of CO₂e.
      </td>

      <td>
        400.2
      </td>
    </tr>

    <tr>
      <td>
        error
      </td>

      <td>
        null / missing if no error.  If error this will contain a string error message
      </td>

      <td>
        null
      </td>
    </tr>
  </tbody>
</Table>

# Example Usage

## Select a single Example Row

```sql
SELECT scope3_v2(
  1000, -- impressions
  DATE('2023-04-20'), -- date
  'nytimes.com', -- inventory_id
  'pc', -- device_type
  null, -- channel
  'US', -- country
  'NY', -- region
  'banner', -- creative_format
  null, -- payload_size
  null, --duration_seconds
  'fixed', -- network
  null, -- seller
  null, -- buying_method
  'deal-1234', -- deal_id
);
```

## Real life usage

```sql
SELECT
	d.app_url,
	d.country,
	d.impressions,
  SELECT scope3_v2(
  	d.impressions, -- impressions
  	d.date, -- date
  	d.app_url, -- inventory_id
  	d.device_type, -- device_type
  	null, -- channel
  	d.country, -- country
  	null, -- region
  	null, -- creative_format
  	null, -- payload_size
  	null, --duration_seconds
  	null, -- network
    null, -- seller
  	null, -- buying_method
  	null, -- deal_id
	):total_emissions
FROM
	delivery_data d;

```

## How to extract single fields

* <https://docs.snowflake.com/en/sql-reference/data-types-semistructured.html#label-data-type-object>
* <https://docs.snowflake.com/en/user-guide/querying-semistructured.html#label-traversing-semistructured-data>

```sql
SELECT
	res:total_emissions,
	res:base_emissions,
	res:supply_path_emissions,
	res:creative_emissions
FROM
	(
    SELECT scope3_v2(
      1000, -- impressions
      DATE('2023-04-20'), -- date
      'nytimes.com', -- inventory_id
      'pc', -- device_type
      null, -- channel
      'US', -- country
      'NY', -- region
      'banner', -- creative_format
      null, -- payload_size
      null, --duration_seconds
      'fixed', -- network
      null, -- seller
      null, -- buying_method
      'deal-1234', -- deal_id
		) res
  )
;

```

### Dealing with errors

If your use case requires special handling for errors, you can use `IFF` and `IS_NULL_VALUE`.

```sql
SELECT
	parse_json('{error: NULL, total_emissions: 123}') as V,
  V:error,
	V:total_emissions,
	IS_NULL_VALUE(v:error),
	IS_NULL_VALUE(v:total_emissions),
	IFF(IS_NULL_VALUE(v:non_existing_key) is null, 'Key does not exist', 'Key exists'),
	IFF(IS_NULL_VALUE(v:error) is null, 'Key does not exist', 'Key exists'),
	IFF(IS_NULL_VALUE(v:total_emissions) is null, 'Key does not exist', 'Key exists')
;
```