In today’s world, crimes are happening at an alarming rate everywhere in the world. Robberies, motor vehicle thefts, various types of assaults, and child abuse are among the most common crimes people face, even in broad daylight. Finding a safe neighborhood is the top priority for anyone who expects to move to a new neighborhood in such a world. Thus, people often need to know how safe their neighborhood is by analyzing the crimes that have been committed in that location. If they know the crime information, they can take the necessary preventive actions to be safe and confident in that environment.
Luckily, many APIs have access to crime data in various locations provided by sources like police departments and other security agencies. Anyone looking to build an application or website that maps crimes in any place can use such APIs. The Crime Data API from RapidAPI is one API you can use to get crime data within a particular neighborhood.
What is the Crime Data API?
Crime Data API is a simple and easy-to-use JSON API that provides crime data in a particular neighborhood within any time range. Using this API, you can create a crime map and visualize the crimes in various geographical areas.
Crime Data API is free and supports multiple programming languages like Python, PHP, Ruby, and Javascript. Let’s learn about this API from this tutorial and use it with different programming languages.
How does the Crime Data API work?
Crime Data API uses simple REST API logic to work. When you call a specific endpoint of this API, the corresponding server will receive the request. The backend server processes it and sends back the necessary output to you as a response. The authentication parameters’ x-RapidAPI-key’ and ‘x-RapidAPI-host’ are included in the request header and used to validate the authenticity of the request. The requests body contains the parameters required to process it.
Once the API server has received the request, its back-end application will process it. The application will retrieve all the crime data from the source database based on the specified criteria and formulate the response. Finally, the server sends the response to the client in JSON format.
Target Audience for the Crime Data API
Application Developers
Application developers aiming to build crime data applications or websites can easily integrate this API. Those applications can notify users about crimes at various locations and ask them to take safety measures. Since this API provides the latitude and longitude of the crime, they can easily map them to the exact location and build a crime map to give the user more informative data. The Crime Data API is free, requires only a few lines of code, and takes only a few minutes to integrate into an application. Since it supports multiple programming languages, it has widespread usability.
Researchers
The API is handy for researchers in the field of crime data analysis. Since this API is free and very easy to use, they can use it directly from the RapidAPI website to get the information they need.
How to connect to the Crime Data API Tutorial – Step by Step
Step 1 – Sign up and Get a RapidAPI Account.
RapidAPI is the world’s largest API marketplace used by more than a million developers worldwide. You can use RapidAPI to search and connect to thousands of APIs using a single SDK, API key, and Dashboard.
To create a RapidAPI account, go to rapidapi.com and click on the Sign Up icon. You can use your Google, Github, or Facebook account for Single Sign-on (SSO) or manually create an account.
Step 2 – Search the API Marketplace
Next, Navigate to the Marketplace and search for “Crime Data API.” Then, select the ‘Crime Data API” from the search results.
Step 3 – Test the API
After loading the API page, you can run a test for the endpoints. For that, navigate to the ‘Endpoints’ section of the API page, which shows all the available endpoints. Select the endpoint you want to test and add the required values for the parameters. Each API endpoint has code snippets in multiple languages, and it automatically adds the API keys and host parameters.
Finally, click on the “Test Endpoint” Button to test the API. Upon successful testing of an API endpoint, you will get the response with a 200 status code. It will output an error code and message if there is an error in the request.
Crime Data API Endpoints
Currently, RapidAPI provides one API endpoint in Crime Data API. It is accessible using the GET request method.
GET /crime
Using this API endpoint, you can get an array of crime data for a particular geographical location. Mandatory parameters are the latitude and longitude of the location and the start and end dates.
The response body contains an array of crime data objects containing the description of the crime, the date and time the crime was committed or reported, and the exact latitude and longitude of the crime location. The description is a short and commonly identifiable name for a particular crime type. For example, BURGLARY – BURGLARY – FORCED ENTRY – RESIDENCE, HARASSING COMMUNICATIONS, VEHICLE THEFT, etc. The following example shows you a set of crime data returned from this endpoint.
Integrating the Crime Data API to an Application
This section will see how to integrate the Crime Data API into a software application with Python, PHP, Ruby, and Javascript providing example code snippets from the API site. We will be using its “GET /crime” endpoint throughout all the code snippets.
In each code, you must define values for the ‘X-RapidAPI-key’ and ‘X-RapidAPI-host’ parameters and include them in the request header. You can take these values from the API website.
Python Code Snippet (requests)
The following python code uses the “requests” library to send a GET request to the endpoint. Hence, you need first to install the module on your computer. Specify all the parameters in the “querystring” variable. Request object’s request method sends the API call. You can also use ‘requests.post’ method for this purpose.
import requests url = "https://jgentes-crime-data-v1.p.rapidapi.com/crime" querystring = {"lat":"37.757815","long":"-122.5076392","startdate":"9/19/2015","enddate":"9/25/2015"} headers = { 'x-rapidapi-key': "cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5", 'x-rapidapi-host': "jgentes-Crime-Data-v1.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
PHP Code Snippet (HTTP v2)
First of all, create a client and a request class object. Then, set the request URL, request method, query string containing the search keyword, and the request header separately from the request object. Finally, send the request through the client objects’ send method. You can also use cURL, HTTP V1, and Unirest/Request module code snippets provided on the API website.
<?php $client = new http\Client; $request = new http\Client\Request; $request->setRequestUrl('https://jgentes-crime-data-v1.p.rapidapi.com/crime'); $request->setRequestMethod('GET'); $request->setQuery(new http\QueryString([ 'lat' => '37.757815', 'long' => '-122.5076392', 'startdate' => '9/19/2015', 'enddate' => '9/25/2015' ])); $request->setHeaders([ 'x-rapidapi-key' => 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host' => 'jgentes-Crime-Data-v1.p.rapidapi.com' ]); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
Ruby (net::http)
Suppose you are using Rubys’ net::http module, import OpenSSL and URI modules in the beginning. In this instance, bypass SSL certification verification by setting it to ‘VERIFY_NONE..’ Unlike in the above code examples, you can directly set the search keyword in the query string along with the URL.
require 'uri' require 'net/http' require 'openssl' url = URI("https://jgentes-crime-data-v1.p.rapidapi.com/crime?lat=37.757815&long=-122.5076392&startdate=9%2F19%2F2015&enddate=9%2F25%2F2015") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-rapidapi-key"] = 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' request["x-rapidapi-host"] = 'jgentes-Crime-Data-v1.p.rapidapi.com' response = http.request(request) puts response.read_body
Javascript (Axios)
The following code snippet shows how to call this API using its ‘axios’ module. You can define the parameters, URL, headers, and the request method in one javascript object and use axios’ request method to send the request and retrieve the response.
import axios from "axios"; const options = { method: 'GET', url: 'https://jgentes-crime-data-v1.p.rapidapi.com/crime', params: { lat: '37.757815', long: '-122.5076392', startdate: '9/19/2015', enddate: '9/25/2015' }, headers: { 'x-rapidapi-key': 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host': 'jgentes-Crime-Data-v1.p.rapidapi.com' } }; axios.request(options).then(function (response) { console.log(response.data); }).catch(function (error) { console.error(error); });
This API belongs to the crime data API category. Following are the similar crime data APIs available on the RapidAPI site.
- CrimeScore API – This API returns a National CrimeScore Safety rating between 1-100 and an A-F grade for a list of USA cities. It uses a propriety methodology based on address-level crime reports, weighted for type of crime, recency, and proximity.
- JailBase API – This API can give you mugshots and arrest information free of charge. Moreover, using this API, you can search for arrested and booked individuals in county jails.
- CrimeoMeter API – Another freemium crime data API that provides you statistics about crimes that happened in a particular area and period. You can get information like the total number of incidents, the total number of incidents per type, and official crime reports for more than 30 cities across the US.
- UK Police Open Data API – A comprehensive crime data API based on data.police.uk. This API can provide helpful information like the outcome of a single crime, a list of crimes and events of an area at a particular date, stop and searches by location, etc.
- SpotCrimes API – You can use this freemium and simple JSON API to search for crime data by a particular coordinate. It will give you helpful information like crime data-id, type, date of the crime and address, etc.
Benefits of the Crime Data API
Easy to Integrate
Crime Data API is a simple API that only needs a few parameters to process the data. Because of that, you do not have to spend a lot of time writing complicated code. Moreover, you can find example code snippets in many programming languages for two or more language frameworks or modules. Because of this simplicity, even someone with little to no background using APIs can master the API reasonably quickly.
Free API
Crime Data API is a simple but valuable API that costs you nothing to get its full capabilities. It will be a huge benefit, particularly for students who learn about APIs and application developers to build innovative applications. In addition, this will be a tremendous support for crime data researchers in their work.
Summary
Crime Data API is a simple yet powerful crime data information API that you can use to build crime data applications and websites. From this article, we discussed its available endpoints, benefits, who can use this, and provided sample code snippets in Python, PHP, Ruby, and Javascript to show how to use this API in an application.
Leave a Reply