Quickstart

Quickstart: call the Credicorp public API from Ruby

Ruby's standard library calls the Credicorp public API without a gem. This quickstart uses net/http and json to list products, parse the envelope and raise on the documented error shape — drop it into a Rails service object or a plain script.

2 min read

stdlibnet/http + json
no gemFaraday optional
symbolizeParse with symbol keys

List products

require 'net/http'
require 'json'
require 'uri'

BASE = 'https://api.credicorp.co.uk/public/v1'

def list_products
  uri = URI("#{BASE}/products")
  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 10) do |http|
    http.get(uri.request_uri, 'Accept' => 'application/json')
  end
  body = JSON.parse(res.body, symbolize_names: true)
  if res.code.to_i >= 400
    e = body[:error] || {}
    raise "#{res.code} #{e[:code]}: #{e[:message]}"
  end
  body[:data]
end

list_products.each do |p|
  puts "#{p[:name]} \u2014 up to \u00a3#{p[:max_amount]}"
end

With Faraday

If your app uses Faraday, the same request is shorter and gives you middleware for retries and JSON parsing:

conn = Faraday.new(url: BASE) { |f| f.response :json }
data = conn.get('products').body['data']

Frequently asked questions

Should I use a gem?

For the public ring, no — net/http is enough. Faraday or HTTParty are fine if your app already depends on them and you want retry middleware.

How do I set a timeout?

Pass read_timeout and open_timeout to Net::HTTP.start, as shown. Never leave a request without a timeout in a web request cycle.

Funding for UK limited companies

Credicorp lends to your company, not to you personally — short-term working capital with no personal guarantee. See what your business could access.