Measurely API

How to interact with the Measurely API to update metric values.

To interact with the Measurely API, you need to send a POST request to the endpoint with an application API key and the metric ID of the value you wish to update. Follow the steps below to understand how to use the API.

Prerequisites

Before you can start using the Measurely API, you will need the following:

  • API Key: You need an application API key to authenticate your request.
  • Metric ID: The ID of the metric that you wish to update.

Remember:

Always keep your API key secure. Do not expose it publicly in your code or repositories.

Making a Request

To update a metric's value, you need to make a POST request to the following endpoint:

POST https://api.measurely.dev/v1/event/{METRIC_ID OR METRIC_NAME}

Request Body

The request body should contain the value you wish to add or remove. The body format should be JSON:

{
  "value": 100
}

Code exemples

export const updateMetric = async (API_KEY, METRIC_ID, value) => {
  const response = await fetch(`https://api.measurely.dev/event/${METRIC_ID}`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${API_KEY}`,
    },
    body: JSON.stringify({ value: value }),
  });
  return response.status;
}
package main

import (
  "bytes"
  "encoding/json"
  "fmt"
  "net/http"
)

func updateMetric(API_KEY string, METRIC_ID string, value int) (int, error) {
  url := fmt.Sprintf("https://api.measurely.dev/event/%s", METRIC_ID)
  jsonData := map[string]interface{}{
    "value": value,
  }
  jsonValue, _ := json.Marshal(jsonData)

  req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
  if err != nil {
    return 0, err
  }

  req.Header.Set("Content-Type", "application/json")
  req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", API_KEY))

  client := &http.Client{}
  resp, err := client.Do(req)
  if err != nil {
    return 0, err
  }
  defer resp.Body.Close()

  return resp.StatusCode, nil
}

Response Codes

The Measurely API responds with different status codes based on the outcome of your request. Here's what each response code means:

200 - Success

The metric value has been successfully updated, and the event summary has been created in the database.

400 - Bad Request

The request is invalid. This may occur due to missing or improperly formatted request data, or an issue with the metric value (e.g., a negative value for a base metric or a value of zero).

401 - Unauthorized

The provided API key or metric ID is invalid, or the request body failed to authenticate.

429 - Too many requests

The rate limit has been exceeded. This typically occurs when the number of requests surpasses the allowed limit for the user's plan.

500 - Internal server error

An error occurred while processing the request, such as a failure to update the metric. This should be very rare.