# Sample Revenue Metrics API Requests

Below are practical examples of how to send requests to the RevenueMetrics API using various popular programming languages, such as Node.js, Python, and C#. These examples demonstrate how to structure your requests, authenticate using an API key, and handle responses effectively.

## &#x20;Node.js Example

```javascript
const fetch = require('node-fetch');

async function fetchRevenueMetrics(apiKey, dateFrom, dateTo) {
  const url = "https://universal-api.myappfree.com/graphql/index.html";
  const headers = {
    "x-api-key": apiKey,
    "Content-Type": "application/json"
  };
  const query = {
    query: `
    query RevenueMetrics($fromDate: String!, $toDate: String!) {
      revenueMetrics(where: { dateFrom: $fromDate, dateTo: $toDate }) {
        total
        items {
          siteId
          siteName
          adunitId
          adunitName
          impressions
          clicks
          revenue
          ecpm
          arpdau
          dau
        }
      }
    }`,
    variables: {
      fromDate: dateFrom,
      toDate: dateTo
    }
  };

  const response = await fetch(url, {
    method: "POST",
    headers: headers,
    body: JSON.stringify(query)
  });

  if (response.ok) {
    const data = await response.json();
    return data;
  } else {
    throw new Error(`Query failed with status ${response.status}: ${await response.text()}`);
  }
}

// Usage example
const apiKey = "YOUR_API_KEY";
const dateFrom = "2025-01-10 00:00:00";
const dateTo = "2025-01-10 10:00:00";

fetchRevenueMetrics(apiKey, dateFrom, dateTo)
  .then(data => {
    console.log("Revenue Metrics:");
    console.log(JSON.stringify(data, null, 2)); // Pretty print the response with indentation
  })
  .catch(err => console.error("Error:", err));
```

## Python Example

```python
import requests
import json

def fetch_revenue_metrics(api_key, date_from, date_to):
    url = "https://universal-api.myappfree.com/graphql/index.html"
    headers = {
        "x-api-key": api_key,
        "Content-Type": "application/json"
    }
    query = {
        "query": """
        query RevenueMetrics($fromDate: String!, $toDate: String!) {
            revenueMetrics(where: { dateFrom: $fromDate, dateTo: $toDate }) {
                total
                items {
                    siteId
                    siteName
                    adunitId
                    adunitName
                    impressions
                    clicks
                    revenue
                    ecpm
                    arpdau
                    dau
                }
            }
        }
        """,
        "variables": {
            "fromDate": date_from,
            "toDate": date_to
        }
    }

    response = requests.post(url, json=query, headers=headers)

    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"Query failed with status code {response.status_code}: {response.text}")

# Usage example
api_key = "YOUR_API_KEY"
date_from = "2025-01-10 00:00:00"
date_to = "2025-01-10 10:00:00"

try:
    data = fetch_revenue_metrics(api_key, date_from, date_to)
    print("Revenue Metrics:")
    print(json.dumps(data, indent=2))  # Pretty print the response
except Exception as e:
    print("Error:", e)
```

## C# Example

```csharp
var client = new RestClient("https://universal-api.myappfree.com/graphql/index.html");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("x-api-key", "YOUR_API_KEY");
request.AddParameter("application/json", @"{
  ""query"": ""query RevenueMetrics($fromDate: String!, $toDate: String!) {
    revenueMetrics(where: { dateFrom: $fromDate, dateTo: $toDate }) {
      total
      items {
        siteId
        siteName
        adunitId
        adunitName
        impressions
        clicks
        revenue
        ecpm
        arpdau
        dau
      }
    }
  }"",
  ""variables"": {
    ""fromDate"": ""2025-01-10 00:00:00"",
    ""toDate"": ""2025-01-10 10:00:00""
  }
}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

## Shell Example

```sh
curl --request POST \
  --url 'https://universal-api.myappfree.com/graphql/index.html' \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: YOUR_API_KEY' \
  --data '{
    "query": "query RevenueMetrics($fromDate: String!, $toDate: String!) {\n      revenueMetrics(where: { dateFrom: $fromDate, dateTo: $toDate }) {\n        total\n        items {\n          siteId\n          siteName\n          adunitId\n          adunitName\n          impressions\n          clicks\n          revenue\n          ecpm\n          arpdau\n          dau\n        }\n      }\n    }",
    "variables": {
      "fromDate": "2025-01-10 00:00:00",
      "toDate": "2025-01-10 10:00:00"
    }
  }'

```


---

# 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://docs.mychips.io/revenue-api/sample-revenue-metrics-api-requests.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.
