Skip to main content
POST
/
v1
/
meters
/
Go (SDK)
package main

import(
	"context"
	"os"
	polargo "github.com/polarsource/polar-go"
	"github.com/polarsource/polar-go/models/components"
	"log"
)

func main() {
    ctx := context.Background()

    s := polargo.New(
        polargo.WithSecurity(os.Getenv("POLAR_ACCESS_TOKEN")),
    )

    res, err := s.Meters.Create(ctx, components.MeterCreate{
        Name: "<value>",
        Filter: components.Filter{
            Conjunction: components.FilterConjunctionOr,
            Clauses: []components.Clauses{
                components.CreateClausesFilterClause(
                    components.FilterClause{
                        Property: "<value>",
                        Operator: components.FilterOperatorNe,
                        Value: components.CreateValueStr(
                            "<value>",
                        ),
                    },
                ),
            },
        },
        Aggregation: components.CreateMeterCreateAggregationAvg(
            components.PropertyAggregation{
                Func: components.FuncMax,
                Property: "<value>",
            },
        ),
        OrganizationID: polargo.Pointer("1dbfc517-0bbf-4301-9ba8-555ca42b9737"),
    })
    if err != nil {
        log.Fatal(err)
    }
    if res.Meter != nil {
        switch res.Meter.Aggregation.Type {
            case components.MeterAggregationTypeAvg:
                // res.Meter.Aggregation.PropertyAggregation is populated
            case components.MeterAggregationTypeCount:
                // res.Meter.Aggregation.CountAggregation is populated
            case components.MeterAggregationTypeMax:
                // res.Meter.Aggregation.PropertyAggregation is populated
            case components.MeterAggregationTypeMin:
                // res.Meter.Aggregation.PropertyAggregation is populated
            case components.MeterAggregationTypeSum:
                // res.Meter.Aggregation.PropertyAggregation is populated
            case components.MeterAggregationTypeUnique:
                // res.Meter.Aggregation.UniqueAggregation is populated
        }

    }
}
import polar_sdk
from polar_sdk import Polar


with Polar(
access_token="<YOUR_BEARER_TOKEN_HERE>",
) as polar:

res = polar.meters.create(request={
"name": "<value>",
"filter_": {
"conjunction": polar_sdk.FilterConjunction.OR,
"clauses": [],
},
"aggregation": {
"func": "count",
},
"organization_id": "1dbfc517-0bbf-4301-9ba8-555ca42b9737",
})

# Handle response
print(res)
import { Polar } from "@polar-sh/sdk";

const polar = new Polar({
accessToken: process.env["POLAR_ACCESS_TOKEN"] ?? "",
});

async function run() {
const result = await polar.meters.create({
name: "<value>",
filter: {
conjunction: "or",
clauses: [
{
property: "<value>",
operator: "ne",
value: "<value>",
},
],
},
aggregation: {
func: "max",
property: "<value>",
},
organizationId: "1dbfc517-0bbf-4301-9ba8-555ca42b9737",
});

console.log(result);
}

run();
declare(strict_types=1);

require 'vendor/autoload.php';

use Polar;
use Polar\Models\Components;

$sdk = Polar\Polar::builder()
->setSecurity(
'<YOUR_BEARER_TOKEN_HERE>'
)
->build();

$request = new Components\MeterCreate(
name: '<value>',
filter: new Components\Filter(
conjunction: Components\FilterConjunction::Or,
clauses: [
new Components\FilterClause(
property: '<value>',
operator: Components\FilterOperator::Ne,
value: '<value>',
),
],
),
aggregation: new Components\PropertyAggregation(
func: Components\Func::Max,
property: '<value>',
),
organizationId: '1dbfc517-0bbf-4301-9ba8-555ca42b9737',
);

$response = $sdk->meters->create(
request: $request
);

if ($response->meter !== null) {
// handle response
}
curl --request POST \
--url https://api.polar.sh/v1/meters/ \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>",
"filter": {
"clauses": [
{
"property": "<string>",
"value": "<string>"
}
]
},
"aggregation": {
"func": "count"
},
"metadata": {},
"unit": "scalar",
"custom_label": "<string>",
"custom_multiplier": 1,
"organization_id": "1dbfc517-0bbf-4301-9ba8-555ca42b9737"
}
'
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: '<string>',
filter: {clauses: [{property: '<string>', value: '<string>'}]},
aggregation: {func: 'count'},
metadata: {},
unit: 'scalar',
custom_label: '<string>',
custom_multiplier: 1,
organization_id: '1dbfc517-0bbf-4301-9ba8-555ca42b9737'
})
};

fetch('https://api.polar.sh/v1/meters/', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
HttpResponse<String> response = Unirest.post("https://api.polar.sh/v1/meters/")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"filter\": {\n \"clauses\": [\n {\n \"property\": \"<string>\",\n \"value\": \"<string>\"\n }\n ]\n },\n \"aggregation\": {\n \"func\": \"count\"\n },\n \"metadata\": {},\n \"unit\": \"scalar\",\n \"custom_label\": \"<string>\",\n \"custom_multiplier\": 1,\n \"organization_id\": \"1dbfc517-0bbf-4301-9ba8-555ca42b9737\"\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://api.polar.sh/v1/meters/")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"<string>\",\n \"filter\": {\n \"clauses\": [\n {\n \"property\": \"<string>\",\n \"value\": \"<string>\"\n }\n ]\n },\n \"aggregation\": {\n \"func\": \"count\"\n },\n \"metadata\": {},\n \"unit\": \"scalar\",\n \"custom_label\": \"<string>\",\n \"custom_multiplier\": 1,\n \"organization_id\": \"1dbfc517-0bbf-4301-9ba8-555ca42b9737\"\n}"

response = http.request(request)
puts response.read_body
{
  "metadata": {},
  "created_at": "2023-11-07T05:31:56Z",
  "modified_at": "2023-11-07T05:31:56Z",
  "id": "<string>",
  "name": "<string>",
  "filter": {
    "clauses": [
      {
        "property": "<string>",
        "value": "<string>"
      }
    ]
  },
  "aggregation": {
    "func": "count"
  },
  "organization_id": "<string>",
  "custom_label": "<string>",
  "custom_multiplier": 123,
  "archived_at": "2023-11-07T05:31:56Z"
}
{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>",
"input": "<unknown>",
"ctx": {}
}
]
}

Authorizations

Authorization
string
header
required

You can generate an Organization Access Token from your organization's settings.

Body

application/json
name
string
required

The name of the meter. Will be shown on customer's invoices and usage.

Minimum string length: 3
filter
Filter · object
required

The filter to apply on events that'll be used to calculate the meter.

aggregation
CountAggregation · object
required

The aggregation to apply on the filtered events to calculate the meter.

metadata
Metadata · object

Key-value object allowing you to store additional information.

The key must be a string with a maximum length of 40 characters. The value must be either:

  • A string with a maximum length of 500 characters
  • An integer
  • A floating-point number
  • A boolean

You can store up to 50 key-value pairs.

unit
enum<string>
default:scalar

The unit of the meter.

Available options:
scalar,
token,
custom
custom_label
string | null

The label for the custom unit, e.g. 'request'. Required when unit is 'custom'.

custom_multiplier
integer | null

The multiplier to convert from the base unit to display scale, e.g. 1000 to display per 1000 units. Defaults to 1 when not provided.

Required range: x > 0
organization_id
string<uuid4> | null

The organization ID.

Example:

"1dbfc517-0bbf-4301-9ba8-555ca42b9737"

Response

Meter created.

metadata
object
required
created_at
string<date-time>
required

Creation timestamp of the object.

modified_at
string<date-time> | null
required

Last modification timestamp of the object.

id
string<uuid4>
required

The ID of the object.

name
string
required

The name of the meter. Will be shown on customer's invoices and usage.

unit
enum<string>
required

The unit of the meter.

Available options:
scalar,
token,
custom
filter
Filter · object
required

The filter to apply on events that'll be used to calculate the meter.

aggregation
CountAggregation · object
required

The aggregation to apply on the filtered events to calculate the meter.

organization_id
string<uuid4>
required

The ID of the organization owning the meter.

custom_label
string | null

The label for the custom unit.

custom_multiplier
integer | null

The multiplier to convert from base unit to display scale.

archived_at
string<date-time> | null

Whether the meter is archived and the time it was archived.