Getting started with the Profile Services API

The Localytics Profile API currently exposes profiles for your organization-wide database as a single RESTful resource at

https://profile.localytics.com/v1/profiles/:customer_id

which accepts the HTTP methods GET, PATCH, and DELETE.

Likewise, profile databases for your apps are accessible via the same RESTful methods at

https://profile.localytics.com/v1/apps/:app_id/profiles/:customer_id

The following Ruby code snippet illustrates how you can quickly start setting and retrieving profile information for your customers.

# Using Ruby 2.1.2require'net/http'require'json'

# All REST methods for the organization-wide database are available at this endpoint:
api_root = "https://profile.localytics.com/v1/profiles"

# Let's call our customer "Isa."  In production, of course, you should not use customer's real names or emails# to identify them, but instead use a secure non-guessable unique identifier like 47c44727-1c22-4dfd-a901-9d0dcf049c89.
customer_id = "Isa"

# Replace these with your real API key and secret:
API_KEY = "MY_API_KEY"
API_SECRET = "MY_API_SECRET"

uri = URI("#{api_root}/#{customer_id}")

Net::HTTP.start(uri.hostname, uri.port, use_ssl: true)do|http|

  # Let's see if a profile for Isa exists:
  get_request = Net::HTTP::Get.new(uri)
  get_request.basic_auth(API_KEY, API_SECRET)

  response = http.request(get_request)

  response.code# 404, because we haven't created this profile yet.

  # Now let's create a profile.  Use PATCH whenever you want to set some profile information.# It will create a new record if it doesn't already exist.
  patch_request = Net::HTTP::Patch.new(uri)
  patch_request.basic_auth(API_KEY, API_SECRET)
  patch_request.content_type = "application/json"

  # Set up some arbitrary profile data for Isa
  profile_attributes = {:name=>"Isa",
    :cats=>["Ofelia", "Mittens", "Spot"],
    :age=>30,
    :"lucky numbers"=>[1, 48, -100, 13],
    :birthday=>"1983-01-01"}

  # The data we want to set must be nested under an "attributes" JSON root node
  patch_request.body = JSON.dump({:attributes => profile_attributes})

  response = http.request(patch_request)

  response.code# 202

  # Now Isa should be there
  response = http.request(get_request)
  response.code# 200
  response.body# {"attributes": {"name": "Isa"...}}

  # We can update and delete Isa's profile attributes:
  updated_attributes = {:age=>31,
    :cats=>nil# Use null to delete individual attributes}
  patch_request.body = JSON.dump({:attributes => updated_attributes})

  http.request(patch_request)
  http.request(get_request)# Isa's age has now been updated and his cats are gone

  # We're done with Isa
  delete_request = Net::HTTP::Delete.new(uri)
  delete_request.basic_auth(API_KEY, API_SECRET)
  http.request(delete_request)end