Getting Started with the Profile Services API

After authenticating, you can quickly start setting and retrieving Profiles information for your customers.

# Using Ruby 2.1.2
require'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 when you want to set some profile information.
  # This will create a new record if the record 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 some profile data for Isa
  profile_attributes = {
    :name=>"Isa",
    :cats=>["Ofelia", "Mittens", "Spot", "Schrödinger"],
    :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 in the database
  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

You've successfully set and deleted Profiles information!