Skip to content

Protecting custom actions

Make sure you’ve first integrated the client-side integration in order to generate the “request tokens” required for each call to the Risk and Filter API.

Often times you will have important user actions in your application that is outside of the scope of the standard Castle events. The Risk API and Filter API APIs both support sending $custom events with a mandatory name parameter.

Or implement it on the frontend

If you have many custom actions in your application that you want to monitor through Castle, but you don’t require an inline response, it will likely be faster for you to roll them out using the client SDKs.

You will need to generate and forward the request_token string from your frontend by using the Browser SDK or a Mobile SDK.

castle = ::Castle::Client.new

begin
  token = request.params['castle_request_token']
  context = Castle::Context::Prepare.call(request)

  res = castle.risk(
    type: '$custom',
    name: 'Added To Cart',
    request_token: token,
    context: {
      ip: context[:ip],
      headers: context[:headers]
    },
    user: {
      id: 'ca1242f498', # Required. A unique, persistent user identifier
      email: 'mgray@example.com' # Recommended
      # See the registration or login guides for more attributes
    },
    properties: {
      item: 'iPhone 13',
      price: 1499.0
    }
  )

  if res[:policy][:action] == 'deny'
    # IMPLEMENT: Deny attempt
  end

rescue Castle::InvalidRequestTokenError
  # Deny attempt. Likely a bad actor bypassing fingerprinting
rescue Castle::Error => e
  # Allow attempt. Data missing or invalid, or a server or timeout error
end