Skip to content

API Client

Peter Jenkins edited this page Mar 16, 2017 · 1 revision

API Client

The SDK provides an interface to the mParticle HTTP API by way of the EventsApi class.

At minimum, the EventsApi must be initialized with an mParticle key and secret. You can find your mParticle key and secret by navigating to the Apps section of the mParticle platform UI.

You must associate your data with the correct key and secret. If your app is multi-platform, for example, be sure to send your Android data to your Android key/secret, and your iOS data to your iOS key/secret.

# set credentials
config = MParticle::Configuration.new
config.api_key = 'REPLACE WITH API KEY'
config.api_secret = 'REPLACE WITH API SECRET'

api_instance = MParticle::EventsApi.new

Uploading Data

The EventsAPI class exposes two interfaces:

  • bulk_upload_events - Accepts up to 100 Batch objects for up to 100 users.
  • upload_events - Accepts a single Batch object for a single user
begin
  # you can also send multiple batches at a time to decrease the amount of network calls
  thread = api_instance.upload_events(batch) { |data, status_code, headers|
    if status_code == 202
      puts "Upload complete"
    end
  }

  # wait for the thread, if needed to prevent the process from terminating
  thread.join

  # alternately, you can omit the callback and synchronously wait until the network request completes
  data, status_code, headers = api_instance.upload_events(batch)
rescue MParticle::ApiError => e
  puts "Exception when calling mParticle: #{e}"
end

Clone this wiki locally