Look up a company by CVR number
ruby
require "net/http"
require "json"
def get_company(cvr)
uri = URI("https://apicvr.dk/api/v1/#{cvr}")
res = Net::HTTP.get_response(uri)
case res
when Net::HTTPOK then JSON.parse(res.body)
when Net::HTTPNotFound then nil
else raise "CVR-API svarede #{res.code}" # 422 eller 502
end
end
company = get_company(24256790)
puts "#{company["name"]}, #{company["zipcode"]} #{company["city"]}"
company["ejere"].each do |owner|
puts "#{owner["name"]} #{owner["ownership_range"]}"
endSearch by company name
Fuzzy search finds companies even when the name is slightly misspelled. Each hit has name, cvr_number, industrycode and industrytext.
ruby
require "net/http"
require "json"
require "erb"
def search(name)
uri = URI("https://apicvr.dk/api/v1/search/fuzzy/#{ERB::Util.url_encode(name)}")
JSON.parse(Net::HTTP.get(uri))
end
search("novo nordisk").first(5).each do |hit|
puts "#{hit["cvr_number"]} #{hit["name"]} - #{hit["industrytext"]}"
endIn a Rails app
Company data rarely changes. Put the lookup in a service and cache the response, so the same CVR number isn't looked up on every request:
ruby
# app/services/cvr_lookup.rb
class CvrLookup
def self.call(cvr)
Rails.cache.fetch("cvr/#{cvr}", expires_in: 6.hours) do
res = Net::HTTP.get_response(URI("https://apicvr.dk/api/v1/#{cvr}"))
res.is_a?(Net::HTTPOK) ? JSON.parse(res.body) : nil
end
end
endHandle errors
| Status | Meaning |
|---|---|
200 | The company was found. The body is JSON. |
404 | No company has that CVR number. Body: {"detail": {"error": "NOT_FOUND"}} |
422 | The CVR number isn't a number. |
502 | The CVR register didn't answer. Try again shortly. |
Always check
protected before using the data for marketing. When it's true, the company has opted out of direct marketing.Frequently asked questions
Can I use Faraday or HTTParty?
Yes. The API is a plain GET request that returns JSON, so any HTTP client works. The examples use Net::HTTP because it needs no gems.
Do I need an API key?
No. CVR-API needs no key and no account.