> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.afternoon.co/llms.txt.
> For full documentation content, see https://docs.afternoon.co/llms-full.txt.

# Synchronously ingest billable events

POST https://api.afternoon.co/v1/events/ingest
Content-Type: application/json

Accepts a single billable event and inserts it synchronously into the database.

Reference: https://docs.afternoon.co/api-reference/afternoon-events-api/events/ingest

## OpenAPI Specification

```yaml
openapi: 3.1.0
info:
  title: Afternoon Events API
  version: 1.0.0
paths:
  /v1/events/ingest:
    post:
      operationId: ingest
      summary: Synchronously ingest billable events
      description: >-
        Accepts a single billable event and inserts it synchronously into the
        database.
      tags:
        - subpackage_events
      parameters:
        - name: Authorization
          in: header
          description: API key obtained from the Afternoon dashboard
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Events processed synchronously
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InngestSyncResponse'
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '413':
          description: Request body too large
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '429':
          description: Rate limit exceeded
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/IngestSyncRequest'
servers:
  - url: https://api.afternoon.co
  - url: http://localhost:8787
components:
  schemas:
    IngestSyncRequest:
      type: object
      properties:
        event_name:
          type: string
          description: Name of the event
        idempotency_key:
          type: string
          description: Unique key to prevent duplicate event processing
        customer_id:
          type: string
          format: cuid
          description: Customer identifier
        timestamp:
          type: string
          format: date-time
          description: Event timestamp in ISO 8601 UTC format (Z or +00:00)
        properties:
          type: object
          additionalProperties:
            description: Any type
          description: Additional event properties
      required:
        - event_name
        - idempotency_key
        - customer_id
        - timestamp
      title: IngestSyncRequest
    InngestSyncResponseStatus:
      type: string
      enum:
        - inserted
        - duplicate
        - failed
      description: Processing outcome for the submitted event
      title: InngestSyncResponseStatus
    InngestSyncResponseCostItemsAccountType:
      type: string
      enum:
        - pricing_unit
        - currency
      description: Wallet denomination type
      title: InngestSyncResponseCostItemsAccountType
    InngestSyncResponseCostItems:
      type: object
      properties:
        amount:
          type: string
          description: Total event cost charged for this denomination
        account_type:
          $ref: '#/components/schemas/InngestSyncResponseCostItemsAccountType'
          description: Wallet denomination type
        pricing_unit_code:
          type: string
          description: Pricing unit code (present for pricing_unit entries)
        currency_code:
          type: string
          description: Currency code (present for currency entries)
      required:
        - amount
        - account_type
      title: InngestSyncResponseCostItems
    InngestSyncResponseCustomerBalancesItemsAccountType:
      type: string
      enum:
        - pricing_unit
        - currency
      description: Wallet denomination type
      title: InngestSyncResponseCustomerBalancesItemsAccountType
    InngestSyncResponseCustomerBalancesItems:
      type: object
      properties:
        balance:
          type: string
          description: Current wallet balance after ingestion
        account_type:
          $ref: >-
            #/components/schemas/InngestSyncResponseCustomerBalancesItemsAccountType
          description: Wallet denomination type
        pricing_unit_code:
          type: string
          description: Pricing unit code (present for pricing_unit entries)
        currency_code:
          type: string
          description: Currency code (present for currency entries)
      required:
        - balance
        - account_type
      title: InngestSyncResponseCustomerBalancesItems
    InngestSyncResponseError:
      type: object
      properties:
        code:
          type: string
        message:
          type: string
      required:
        - code
        - message
      title: InngestSyncResponseError
    InngestSyncResponse:
      type: object
      properties:
        success:
          type: boolean
        status:
          $ref: '#/components/schemas/InngestSyncResponseStatus'
          description: Processing outcome for the submitted event
        usage_id:
          type: string
          description: Created or existing billable event ID
        cost:
          type: array
          items:
            $ref: '#/components/schemas/InngestSyncResponseCostItems'
          description: >-
            Aggregated event cost by wallet denomination. Present on successful
            requests.
        customer_balances:
          type: array
          items:
            $ref: '#/components/schemas/InngestSyncResponseCustomerBalancesItems'
          description: >-
            Current customer wallet balances across denominations after
            ingestion.
        error:
          $ref: '#/components/schemas/InngestSyncResponseError'
        request_id:
          type: string
          description: Request ID for tracing
      required:
        - success
        - status
        - request_id
      title: InngestSyncResponse
    ErrorResponseError:
      type: object
      properties:
        code:
          type: string
        message:
          type: string
        details:
          oneOf:
            - description: Any type
            - type: 'null'
      required:
        - code
        - message
      title: ErrorResponseError
    ErrorResponse:
      type: object
      properties:
        success:
          type: boolean
        error:
          $ref: '#/components/schemas/ErrorResponseError'
        request_id:
          type: string
          description: Request ID for tracing
      required:
        - success
        - error
      title: ErrorResponse
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: API key obtained from the Afternoon dashboard

```

## SDK Code Examples

```typescript
import { AfternoonClient } from "afternoon-sdk";

async function main() {
    const client = new AfternoonClient({
        token: "YOUR_TOKEN_HERE",
    });
    await client.events.ingest({
        eventName: "api.request",
        idempotencyKey: "evt_abc123xyz",
        customerId: "cjld2cjxh0000qzrmn831i7rn",
        timestamp: new Date("2023-11-07T05:31:56Z"),
    });
}
main();

```

```python
import requests

url = "https://api.afternoon.co/v1/events/ingest"

payload = {
    "event_name": "api.request",
    "idempotency_key": "evt_abc123xyz",
    "customer_id": "cjld2cjxh0000qzrmn831i7rn",
    "timestamp": "2023-11-07T05:31:56Z"
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.afternoon.co/v1/events/ingest"

	payload := strings.NewReader("{\n  \"event_name\": \"api.request\",\n  \"idempotency_key\": \"evt_abc123xyz\",\n  \"customer_id\": \"cjld2cjxh0000qzrmn831i7rn\",\n  \"timestamp\": \"2023-11-07T05:31:56Z\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.afternoon.co/v1/events/ingest")

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  \"event_name\": \"api.request\",\n  \"idempotency_key\": \"evt_abc123xyz\",\n  \"customer_id\": \"cjld2cjxh0000qzrmn831i7rn\",\n  \"timestamp\": \"2023-11-07T05:31:56Z\"\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.afternoon.co/v1/events/ingest")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"event_name\": \"api.request\",\n  \"idempotency_key\": \"evt_abc123xyz\",\n  \"customer_id\": \"cjld2cjxh0000qzrmn831i7rn\",\n  \"timestamp\": \"2023-11-07T05:31:56Z\"\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.afternoon.co/v1/events/ingest', [
  'body' => '{
  "event_name": "api.request",
  "idempotency_key": "evt_abc123xyz",
  "customer_id": "cjld2cjxh0000qzrmn831i7rn",
  "timestamp": "2023-11-07T05:31:56Z"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.afternoon.co/v1/events/ingest");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"event_name\": \"api.request\",\n  \"idempotency_key\": \"evt_abc123xyz\",\n  \"customer_id\": \"cjld2cjxh0000qzrmn831i7rn\",\n  \"timestamp\": \"2023-11-07T05:31:56Z\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "event_name": "api.request",
  "idempotency_key": "evt_abc123xyz",
  "customer_id": "cjld2cjxh0000qzrmn831i7rn",
  "timestamp": "2023-11-07T05:31:56Z"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.afternoon.co/v1/events/ingest")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```