🎉 ezePing 1.0 alpha is released. Read more →

Swift (using URLSession)

Prerequisites

  • ezePing API Key, available instantly after creating an account. You can copy it from here (opens in a new tab).
  • Event Category Name is the category to which you want to send this event. If you haven't created an event category yet, you can create a new category here (opens in a new tab).

Payload

You can send any payload, as long as it is in a valid JSON format.

json
'fields': {
  'amount': 50,
  'plan': 'PRO',
  'email': 'user@email.com'
}

Installation

Swift
import Foundation
 
let url = URL(string: "https://www.ezeping.com/api/v1/events")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("Bearer <YOUR_API_KEY>", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
 
let data: [String: Any] = [
    "category": "<YOUR_CATEGORY_NAME>",
    "fields": [
        "field1": "value1",  // For example, user ID
        "field2": "value2"   // For example, user email
    ]
]
 
do {
    request.httpBody = try JSONSerialization.data(withJSONObject: data, options: [])
 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        if let error = error {
            print("Request failed with error:", error)
            return
        }
 
        if let data = data,
           let responseString = String(data: data, encoding: .utf8) {
            print("Response:", responseString)
        }
    }
 
    task.resume()
} catch {
    print("Failed to encode JSON:", error)
}