---
layout: article
title: Track events from your server
description: Send Appwrite Analytics events from your backend with an API key, and attach server-side actions such as purchases to the visitor who made them.
---

Some of the events you care about happen on your server: an order that completes, a subscription that renews, a job that finishes. Send these from your backend with an API key. When you pass the visitor's IP address and user agent, the event joins that visitor's visit, next to the pageviews their browser sent.

# Create an API key

Server-side events use an API key with the `analytics.write` scope.

1. In your project, open **API Keys** and click **Create API key**.
2. Enter a name and select the `analytics.write` scope.
3. Create the key and copy its secret.

Keep the key on your server. A key with `analytics.write` can also create, change, and delete analytics properties.

# Send an event

Call `createEvent` with the property ID, the event name, and the URL the event belongs to.

```server-nodejs
import { Client, Analytics } from 'node-appwrite';

const client = new Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>')
    .setKey('<API_KEY>');

const analytics = new Analytics(client);

await analytics.createEvent({
    propertyId: '<PROPERTY_ID>',
    name: 'purchase_completed',
    url: 'https://example.com/checkout',
    userId: '<USER_ID>',
    ip: '<VISITOR_IP>',
    userAgent: '<VISITOR_USER_AGENT>',
    props: ['plan', 'pro', 'interval', 'yearly'],
});
```
```server-deno
import { Client, Analytics } from 'npm:node-appwrite';

const client = new Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>')
    .setKey('<API_KEY>');

const analytics = new Analytics(client);

await analytics.createEvent({
    propertyId: '<PROPERTY_ID>',
    name: 'purchase_completed',
    url: 'https://example.com/checkout',
    userId: '<USER_ID>',
    ip: '<VISITOR_IP>',
    userAgent: '<VISITOR_USER_AGENT>',
    props: ['plan', 'pro', 'interval', 'yearly'],
});
```
```server-php
<?php

require 'vendor/autoload.php';

use Appwrite\Client;
use Appwrite\Services\Analytics;

$client = (new Client())
    ->setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    ->setProject('<PROJECT_ID>')
    ->setKey('<API_KEY>');

$analytics = new Analytics($client);

$analytics->createEvent(
    propertyId: '<PROPERTY_ID>',
    name: 'purchase_completed',
    url: 'https://example.com/checkout',
    userId: '<USER_ID>',
    ip: '<VISITOR_IP>',
    userAgent: '<VISITOR_USER_AGENT>',
    props: ['plan', 'pro', 'interval', 'yearly'],
);
```
```server-python
from appwrite.client import Client
from appwrite.services.analytics import Analytics

client = Client()
client.set_endpoint('https://<REGION>.cloud.appwrite.io/v1')
client.set_project('<PROJECT_ID>')
client.set_key('<API_KEY>')

analytics = Analytics(client)

analytics.create_event(
    property_id='<PROPERTY_ID>',
    name='purchase_completed',
    url='https://example.com/checkout',
    user_id='<USER_ID>',
    ip='<VISITOR_IP>',
    user_agent='<VISITOR_USER_AGENT>',
    props=['plan', 'pro', 'interval', 'yearly'],
)
```
```server-ruby
require 'appwrite'

include Appwrite

client = Client.new
    .set_endpoint('https://<REGION>.cloud.appwrite.io/v1')
    .set_project('<PROJECT_ID>')
    .set_key('<API_KEY>')

analytics = Analytics.new(client)

analytics.create_event(
    property_id: '<PROPERTY_ID>',
    name: 'purchase_completed',
    url: 'https://example.com/checkout',
    user_id: '<USER_ID>',
    ip: '<VISITOR_IP>',
    user_agent: '<VISITOR_USER_AGENT>',
    props: ['plan', 'pro', 'interval', 'yearly']
)
```
```server-dotnet
using Appwrite;
using Appwrite.Services;

var client = new Client()
    .SetEndpoint("https://<REGION>.cloud.appwrite.io/v1")
    .SetProject("<PROJECT_ID>")
    .SetKey("<API_KEY>");

var analytics = new Analytics(client);

await analytics.CreateEvent(
    propertyId: "<PROPERTY_ID>",
    name: "purchase_completed",
    url: "https://example.com/checkout",
    userId: "<USER_ID>",
    ip: "<VISITOR_IP>",
    userAgent: "<VISITOR_USER_AGENT>",
    props: new List<string> { "plan", "pro", "interval", "yearly" }
);
```
```server-dart
import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> main() async {
  final client = Client()
      .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
      .setProject('<PROJECT_ID>')
      .setKey('<API_KEY>');

  final analytics = Analytics(client);

  await analytics.createEvent(
    propertyId: '<PROPERTY_ID>',
    name: 'purchase_completed',
    url: 'https://example.com/checkout',
    userId: '<USER_ID>',
    ip: '<VISITOR_IP>',
    userAgent: '<VISITOR_USER_AGENT>',
    props: ['plan', 'pro', 'interval', 'yearly'],
  );
}
```
```server-kotlin
import io.appwrite.Client
import io.appwrite.services.Analytics

suspend fun main() {
    val client = Client()
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
        .setProject("<PROJECT_ID>")
        .setKey("<API_KEY>")

    val analytics = Analytics(client)

    analytics.createEvent(
        propertyId = "<PROPERTY_ID>",
        name = "purchase_completed",
        url = "https://example.com/checkout",
        userId = "<USER_ID>",
        ip = "<VISITOR_IP>",
        userAgent = "<VISITOR_USER_AGENT>",
        props = listOf("plan", "pro", "interval", "yearly"),
    )
}
```
```server-swift
import Appwrite

let client = Client()
    .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
    .setProject("<PROJECT_ID>")
    .setKey("<API_KEY>")

let analytics = Analytics(client)

_ = try await analytics.createEvent(
    propertyId: "<PROPERTY_ID>",
    name: "purchase_completed",
    url: "https://example.com/checkout",
    props: ["plan", "pro", "interval", "yearly"],
    userId: "<USER_ID>",
    ip: "<VISITOR_IP>",
    userAgent: "<VISITOR_USER_AGENT>"
)
```
```server-go
package main

import (
    "log"

    "github.com/appwrite/sdk-for-go/v7/appwrite"
)

func main() {
    client := appwrite.NewClient(
        appwrite.WithEndpoint("https://<REGION>.cloud.appwrite.io/v1"),
        appwrite.WithProject("<PROJECT_ID>"),
        appwrite.WithKey("<API_KEY>"),
    )

    analytics := appwrite.NewAnalytics(client)

    _, err := analytics.CreateEvent(
        "<PROPERTY_ID>",
        "purchase_completed",
        "https://example.com/checkout",
        analytics.WithCreateEventUserId("<USER_ID>"),
        analytics.WithCreateEventIp("<VISITOR_IP>"),
        analytics.WithCreateEventUserAgent("<VISITOR_USER_AGENT>"),
        analytics.WithCreateEventProps([]string{"plan", "pro", "interval", "yearly"}),
    )
    if err != nil {
        log.Fatal(err)
    }
}
```
```server-rust
use appwrite::Client;
use appwrite::services::Analytics;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    client.set_endpoint("https://<REGION>.cloud.appwrite.io/v1");
    client.set_project("<PROJECT_ID>");
    client.set_key("<API_KEY>");

    let analytics = Analytics::new(&client);

    analytics.create_event(
        "<PROPERTY_ID>",
        "purchase_completed",
        "https://example.com/checkout",
        None, // domain
        None, // referrer
        None, // screen_width
        None, // session_hash
        None, // scroll_depth
        None, // engagement_time
        Some(vec!["plan".into(), "pro".into(), "interval".into(), "yearly".into()]),
        Some("<USER_ID>"),
        Some("<VISITOR_IP>"),
        Some("<VISITOR_USER_AGENT>"),
    ).await?;

    Ok(())
}
```
```bash
appwrite client \
    --endpoint https://<REGION>.cloud.appwrite.io/v1 \
    --project-id '<PROJECT_ID>' \
    --key '<API_KEY>'

appwrite analytics create-event \
    --property-id '<PROPERTY_ID>' \
    --name purchase_completed \
    --url https://example.com/checkout \
    --user-id '<USER_ID>' \
    --ip '<VISITOR_IP>' \
    --user-agent '<VISITOR_USER_AGENT>' \
    --props plan --props pro --props interval --props yearly
```
```curl
curl -X POST 'https://<REGION>.cloud.appwrite.io/v1/analytics/properties/<PROPERTY_ID>/events' \
    -H 'Content-Type: application/json' \
    -H 'X-Appwrite-Project: <PROJECT_ID>' \
    -H 'X-Appwrite-Key: <API_KEY>' \
    -d '{
        "name": "purchase_completed",
        "url": "https://example.com/checkout",
        "userId": "<USER_ID>",
        "ip": "<VISITOR_IP>",
        "userAgent": "<VISITOR_USER_AGENT>",
        "props": ["plan", "pro", "interval", "yearly"]
    }'
```

The event takes the same parameters as a client-side event. See [Send events over REST](/docs/products/analytics/rest#parameters).

# Attribute the event to a visitor

Three parameters are available only with an API key that has the `analytics.write` scope:

- `ip`: the visitor's IP address. Appwrite uses it for the visitor's location and identity in place of your server's address.
- `userAgent`: the visitor's `User-Agent` header. Appwrite uses it for the browser, operating system, and device, and for the visitor's identity.
- `userId`: your own ID for the user. Appwrite stores it with the event.

Appwrite identifies a visitor by their IP address, user agent, and the hostname of the event URL. When `ip`, `userAgent`, and the URL's hostname match what the visitor's browser sent, the server event counts toward the same visitor, and toward the same visit while that visit is active. For how visits start and end, see [Visitors and sessions](/docs/products/analytics/visitors).

Read the IP address and user agent from the request the visitor made to your server, and pass them on. Behind a load balancer or CDN, take the IP address from the header your platform sets for the client address, such as `CF-Connecting-IP` on Cloudflare. In `X-Forwarded-For`, use the address your own proxy added, not the first one, which the client can set.

Without `ip` and `userAgent`, Appwrite records the event as a visit from your server: located where your server runs, with your HTTP client's user agent, and counted as a visitor of its own.

# Errors

| Status | Type | Cause |
| --- | --- | --- |
| `400` | `general_argument_invalid` | A parameter is missing or invalid. See [Event limits](/docs/products/analytics/limits#event-limits). |
| `401` | `general_access_forbidden` | The request sets `userId`, `ip`, or `userAgent`, and its API key is missing or doesn't have the `analytics.write` scope. |
| `404` | `analytics_property_not_found` | The property ID is wrong, or the property is turned off. |

A request with an `analytics.write` key skips the property's allowed origins, so Appwrite accepts server-side events from wherever your server runs.
