// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples

[source, js]
----
const response = await client.security.queryApiKeys({
  size: 0,
  query: {
    bool: {
      must: {
        term: {
          invalidated: false,
        },
      },
      should: [
        {
          range: {
            expiration: {
              gte: "now",
            },
          },
        },
        {
          bool: {
            must_not: {
              exists: {
                field: "expiration",
              },
            },
          },
        },
      ],
      minimum_should_match: 1,
    },
  },
  aggs: {
    keys_by_username: {
      composite: {
        sources: [
          {
            usernames: {
              terms: {
                field: "username",
              },
            },
          },
        ],
      },
      aggs: {
        expires_soon: {
          filter: {
            range: {
              expiration: {
                lte: "now+30d/d",
              },
            },
          },
          aggs: {
            key_names: {
              terms: {
                field: "name",
              },
            },
          },
        },
      },
    },
  },
});
console.log(response);
----
