Knowing which company a prospective user is from can be invaluable. Use it to prioritise your leads, tailor your responses, or just monitor usage of your website. Many web analytics services offer some kind of organisation tracking data, but how do they do it, and how can we use it ourselves?
What are ASNs?
Autonomous System Numbers are used by all services which look up company details by IP address. They identify the organisation or Internet Service Provider which controls a set of IP addresses. They’re used in BGP to route information to the correct network. Unless you’re a network administrator, you don’t need to understand the specifics, but they’re essential for looking up company details.
By keeping an up-to-date list of ASNs, it’s possible to look up a specific IP address and establish which organisation owns it. In most cases, we aren’t interested in situations where an ISP, such as Comcast or Virgin Media, owns the IP address, but the information can still be valuable to us. Maintaining this list is a lot of work, so most people opt to use a provider, like ipdata, to look up ASN details, along with other IP address metadata, such as location.
Looking up company details
ipdata provides a simple API to look up ASN details. Let’s try it out with a few examples:
Microsoft Cloud hosting IP
curl "https://api.ipdata.co/13.107.6.152/asn?api-key=test"
{
"asn": "AS8068",
"name": "Microsoft Corporation",
"domain": "microsoft.com",
"route": "13.107.6.0/24",
"type": "hosting"
}
AT&T IP
curl "https://api.ipdata.co/32.100.100.100/asn?api-key=test"
{
"asn": "AS7018",
"name": "AT&T Services, Inc.",
"domain": "att.com",
"route": "32.96.0.0/13",
"type": "isp"
}
UK Government (House of Commons) IP
curl "https://api.ipdata.co/194.60.38.10/asn?api-key=test"
{
"asn": "AS29214",
"name": "THE HOUSE OF COMMONS",
"domain": "parliament.uk",
"route": "194.60.38.0/24",
"type": "isp"
}
As you can see, each IP returns the ASN associated with the IP, along with additional data about the owner. We’re able to know when our website is being used by UK Parliament or many other organisations, businesses, or governments.
ASN Type
A unique feature of ipdata is the ASN type. We attempt to match each ASN to the type of entity which owns it. The possible types are as follows:
TypeDescriptionhostingBelonging to a datacenter (we have mapped 200M+ hosting IP addresses)ispBelonging to ISP IP space (we have mapped 1B+ ISP IPs)eduEducational institutionsgovOne of ~25,000 government agencies worldwidemilMilitary organizationsbusinessEnd-user organizations
We’re mostly interested in the business
type for lead prioritisation. However, due to the large number of ASNs, there will always be small corrections or mistakes, meaning the type may be wrong for some IPs. In the examples above, the House of Commons IP should likely be mapped to gov
, but is reported as an isp
, as that’s how it’s represented in public records. For this reason, the type should be used as an indicator, rather than a fact.
The hosting
IPs are accurate and include most cloud hosting providers. This can be useful if you want to block crawlers, scrapers and bots from hosting providers. The first example shows the results for a request from one of Microsoft’s backend services.
Enriching a “Contact Us” form with company details
Let’s build a quick practical example. We have a “Contact Us” form on our website, allowing potential customers to get in touch. We want to include the company details of the user contacting us, without asking them to fill in additional fields.
<form action="/contact" method="POST">
<input name="email" type="email" placeholder="Email address" />
<textarea name="message">Your message here...</textarea>
<input type="submit" />
</form>
Here’s a Node.js/Express application to handle serving the contact form, and handle contact messages, emailing our contact@dogtreats.com email with the details.
const express = require("express");
const app = express();
const axios = require("axios");
const nodemailer = require("nodemailer");
const bodyParser = require("body-parser");
const email = nodemailer.createTransport({
// https://nodemailer.com
sendmail: true,
});
// Get an ipdata API Key from here: https://ipdata.co/sign-up.html
const IPDATA_API_KEY = "test";
const getIpData = async (ip) => {
ip = "81.107.235.77";
const response = await axios.get(
`https://api.ipdata.co/${ip}?api-key=${IPDATA_API_KEY}`
);
return response.data;
};
// Serve the contact page
app.get("/contact", (req, res) =>
res.sendFile("./contact.html", { root: __dirname })
);
// Handle a contact request
app.post("/contact", bodyParser.urlencoded(), async (req, res) => {
const ip = req.connection.remoteAddress;
const ipdata = await getIpData(ip);
const message = `From: ${req.body.email}
Message:
${req.body.message}
Company details:
Name: ${ipdata.asn.name}
Domain: ${ipdata.asn.domain}
Type: ${ipdata.asn.type}
`;
await email.sendMail({
from: req.body.email,
to: "contact@dogtreats.com",
subject: "Contact Us Message",
text: message,
});
res.status(200).send("Thanks for your message!");
});
app.listen(8000);
The email sent to contact@dogtreats.com looks like this:
Of course, this could all use some styling to make it more attractive! However, the functionality is all there and in this example, we’re able to see when someone from within the House of Commons is asking for some dog treats.
Conclusions
With a very small amount of code, we’ve been able to enrich our Contact Us form with company details with just an IP address. Using the ipdata API, we can also include other useful information, such as their location, language and currency.
The uses of company details go beyond a simple contact us form, allowing advanced lead prioritisation and much more. Let us know how you’re using it in the comments below!