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.
Before you can start using the Measurely API, you will need the following:
Always keep your API key secure. Do not expose it publicly in your code or repositories.
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}
The request body should contain the value you wish to add or remove. The body format should be JSON:
{
"value": 100
}
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
}
The Measurely API responds with different status codes based on the outcome of your request. Here's what each response code means:
The metric value has been successfully updated, and the event summary has been created in the database.
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).
The provided API key or metric ID is invalid, or the request body failed to authenticate.
The rate limit has been exceeded. This typically occurs when the number of requests surpasses the allowed limit for the user's plan.
An error occurred while processing the request, such as a failure to update the metric. This should be very rare.