# API Module

```
import api from 'api';
```

### Methods

[logs(options)](https://docs.medable.com/reference#section-logs-options-)\
[stats(options)](https://docs.medable.com/reference#section-stats-options-)

### logs(options)

Queries the org's log. Very useful for creating daily activity reports, searching for access anomalies, etc.

**Arguments**

* `options` (Object)
  * `where`
  * `map`
  * `group`
  * `sort`
  * `limit`
  * `skip`

### stats(options)

Queries the org's stats.

**Arguments**

* `options` (Object)
  * `where`
  * `map`
  * `group`
  * `sort`
  * `limit`
  * `skip`

### Examples

The following example finds statistics on all the scripts that ran yesterday (does not factor in script errors or local time).Logs Example

```
import api from 'api'
import moment from 'moment'


let startId = new ObjectID(moment(new Date()).utc().subtract(1,'day').startOf('day').toDate())
let endId = new ObjectID(moment(new Date()).utc().subtract(1,'day').endOf('day').toDate())

return api.logs({
    where: {
        _id: {$gte: startId, $lte: endId},
        src: consts.logs.sources.script
    }
})
```

Logs Example Response

```
{
    "data": [
        {
            "_id": "59486583514ef1010000388f",
            "beg": "2017-06-20T00:00:03.161Z",
            "cms": 0,
            "ctt": 0,
            "end": "2017-06-20T00:00:03.308Z",
            "err": {
                "faults": []
            },
            "in": 0,
            "lvl": 2,
            "oid": "5888b91d35f8c91536c6ddea",
            "ops": 11852,
            "out": 0,
            "pid": "5888b91d35f8c91536c6ddea",
            "pis": [],
            "pts": [],
            "req": "59486583fc65ed0100bf97c9",
            "sid": "589a14f9494aeb77296e4309",
            "src": 2,
            "stp": "job",
            "sts": 200,
            "trc": []
        }
    ]
}
```

The following example returns the login statistics for the previous two days.Stats Example

```
import api from 'api'
import moment from 'moment'

let starting = moment(new Date()).utc().subtract(2,'day').startOf('day').toDate()

return api.stats({
    where: {
        starting: {$gte: starting},
        code: consts.stats.sources.logins
    }
})
```

Stats Example Response

```
{
    "data": [
        {
            "_id": "59237b84967e8b6cd67ed775",
            "active": 35,
            "code": 3,
            "ending": "2017-05-22T23:59:59.999Z",
            "object": "stat",
            "org": "58208081214454926424e515",
            "starting": "2017-05-22T00:00:00.000Z",
            "today": 1
        },
        {
            "_id": "5924cd03967e8b6cd6bb7a52",
            "active": 35,
            "code": 3,
            "ending": "2017-05-23T23:59:59.999Z",
            "object": "stat",
            "org": "58208081214454926424e515",
            "starting": "2017-05-23T00:00:00.000Z",
            "today": 1
        }
    ]
}
```


---

# 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.medable.com/cortex-api/scripting/modules-1/api-module.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.
