Daily prayers are an important part of the Islamic faith. Islamic followers usually do several prayers at different times throughout the day, which are determined based on the geographic location and sun’s positioning. In Islamic countries, they usually announce the call to prayers (adhan) daily. But, if an Islamic person lives in a non-Islamic country, they will not hear it. Thus, it will be helpful for them to have a method to know the prayer times of the location daily.
Fortunately, there are apps built for this purpose that use APIs to retrieve the prayer times according to the country. Prayer Times API is one such API that can retrieve the prayer times by city or address.
What is Prayer Times API?
Prayer Times API is a simple and flexible API from RapidAPI that provides Islamic prayer times for a given day or a calendar for a whole month. When you make a call to this API, it will return prayer times by city, country, or a specific address in JSON format.
The API is completely free and supports various programming languages, including Python, PHP, Ruby, and Javascript. In this article, we will see how to use this API with multiple programming languages.
How does the Prayer Times API work?
Prayer Times API uses simple API logic such as sending a request to the API and obtaining the necessary response from the API. The body of the API request contains the required parameters which specify the customizations to generate the prayer times. When you include the authentication parameters such as API key and host when sending a request, the backend server will identify it as valid. Once the API server has received and processed the request, it will send back the appropriate response to the client in JSON format.
If the request is successful, it will display the required information. For example, a request to prayer times by the city endpoint will generate the list of prayer times for that city. If the request is invalid or unsuccessful, the response payload will contain the error code and the details.
Target Audience for the Prayer Times API
Religious App Developers
Religious App developers can integrate this API to attract Islamic audiences both living in non-Islamic and Islamic countries. They can build a useful app to notify them of the exact prayer times in their location, which will help them in their busy life schedule. Also, academic institutions can use such apps to know the prayer times and allow Islamic students to go out for prayers accordingly. This API is free, each endpoint requires them to write only a few lines of code, and integration to an application is pretty straightforward. It also supports multiple programming languages. Therefore, they can easily integrate into different platforms.
Researchers
Apart from using apps developed based on this API, researchers can get information straight from this API for any research regarding prayers. Since this API is completely free, they can get the required information at no cost.
How to connect to the Prayer Times 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 around the world. 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 create an account manually.
Step 2 – Search the API Marketplace
Navigate to the Marketplace and search for “Prayer Times API.” Then, select the Prayer Times AP from the search results.
Step 3 – Test the API
After subscribing to the API, it is important to run a test for the endpoints. For that, navigate to the Endpoints section of the API page. This section lists all the available endpoints.
Next, select the endpoint you want to test and add the required details. In this instance, we will select the ‘GET Prayer Times Calendar by city’ endpoint to test. Each API endpoint has code snippets in multiple languages, and it automatically adds the API keys.
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.
Prayer Times API Endpoints
Currently, Prayer Times API has four endpoints, as shown in the following image.
GET Prayer Time Calendar by City
This endpoint returns a prayer times calendar for a given month, year, and city. You need to specify the required parameters country, year, month, and city. Specify the country in ISO two-letter format or the name. For example, ‘US’ or ‘United States of America.’ Include the month as a number between 1-12 corresponding to each month. The following are the optional parameters.
- latitudeAdjustmentMethod – Method for adjusting times at higher latitudes – for example, checking timings in the UK or Sweden. 1 – Middle of the Night 2 – One Seventh 3 – Angle Based
- School – 1 for Hanfi. 0 for all others, including Shafi, Hanbali, etc.
- Method- Any of the prayer time calculation methods specified on https://aladhan.com/calculation-methods
- State – US state name or abbreviation. Example: ‘Colorado’ or ‘CO’
The response will contain the “timings” object consisting of prayer times for different times of the day like “Fajr,” “Sunrise,” “Dhuhr,” “Asr, “Sunset,” etc. for the whole month.
GET Prayer Times by City
This endpoint fetches the prayer times for a given day by city and country. It requires the city and the country as the parameters. In addition to that, you can specify the ’latitudeAdjustmentMethod,’ ‘school,’ ‘method,’ and ‘state’ parameters. For the ‘school’ parameter, you can either specify 0 for Shafi or 1 for Hanafi.
GET Calendar by Address
This endpoint returns the prayer times calendar for a month by address. The ‘month,’ ‘year,’ and the ‘address are required parameters, and ’latitudeAdjustmentMethod’ and ‘school’ are optional parameters. The output will contain the “timings” object consisting of prayer times for the particular address you specify.
GET Prayer Times by Address
This endpoint provides all the prayer times for a specific date at a specific address. The only parameter you have to specify is the address that you need to get the prayer times. In addition to that, you can specify the ’latitudeAdjustmentMethod,’ ‘school,’ ‘method,’ and ‘state’ parameters.
Integrating Prayer Times API to an Application
This section will see how to integrate the Prayer Times API into a software application with different programming languages such as Python, PHP, Ruby, and Javascript. To execute these code snippets, first, you need to install the specific programming language on your computer. We will be using the “Prayer Times Calendar by city” GET endpoint for all the code snippets.
You can simply integrate the following code snippets into your application based on the application language. In each code, you need to include the API key and API-host parameters in the header.
Python Code Snippet (requests)
The following python code uses the “requests” library to send a GET request to the endpoint. Define the mandatory search term in the “query string” variable. Then use the URL, header, and the query string in the Request object’s request method to send the API call programmatically. You can also use requests.post method for this purpose.
import requests url = "https://aladhan.p.rapidapi.com/calendarByCity" querystring = {"country":"US","year":"2021","month":"9","city":"Denver"} headers = { 'x-rapidapi-host': "aladhan.p.rapidapi.com", 'x-rapidapi-key': "cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
PHP Code Snippet (HTTP v2)
First, create a client and a request class object. Then you need to set the API url, request method, query string containing the search keyword, and the header separately from the request object. Finally, send the request through the client objects’ send method.
<?php $client = new http\Client; $request = new http\Client\Request; $request->setRequestUrl('https://aladhan.p.rapidapi.com/calendarByCity'); $request->setRequestMethod('GET'); $request->setQuery(new http\QueryString([ 'country' => 'US', 'year' => '2021', 'month' => '9', 'city' => 'Denver' ])); $request->setHeaders([ 'x-rapidapi-host' => 'aladhan.p.rapidapi.com', 'x-rapidapi-key' => 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' ]); $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://aladhan.p.rapidapi.com/calendarByCity?country=US&year=2021&month=9&city=Denver") 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-host"] = 'aladhan.p.rapidapi.com' request["x-rapidapi-key"] = 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' response = http.request(request) puts response.read_body
Javascript (Axios)
Integrating this API into a Javascript application is pretty straightforward and requires a few lines of code. 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. The request method is to send the request and retrieve the response.
import axios from "axios"; const options = { method: 'GET', url: 'https://aladhan.p.rapidapi.com/calendarByCity', params: {country: 'US', year: '2021', month: '9', city: 'Denver'}, headers: { 'x-rapidapi-host': 'aladhan.p.rapidapi.com', 'x-rapidapi-key': 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' } }; axios.request(options).then(function (response) { console.log(response.data); }).catch(function (error) { console.error(error); });
Alternative to Prayer Times API
Muslim Salat API – This API will provide the prayer time based on the location, date, and time. You can get the prayer times from this API by yearly, monthly, weekly, and daily.
Salah API – You can use this simple API to get prayer times based on a specific timezone, date, latitude, and longitude.
Prayer Times – This is another simple and free API to convert prayer times in any location
Benefits of the Prayer Times API
Easy to Integrate
Every endpoint of Prayer Times API requires just a few parameters. Hence, it is easy to integrate into any application. In addition, you can find example code snippets in many programming languages with two or more frameworks or modules. Because of this simplicity, even someone with little to no background using APIs can master the API reasonably quickly.
Customizations
You can define several customizations such as ’latitudeAdjustmentMethod,’ ‘school,’ ‘method,’ and ‘state’ so Islamic people can get prayer times based on different criteria. Application developers can provide different functionalities because of these different parameters.
Free API
Prayer Times API allows app developers to get its full capabilities free of charge. It will be a huge benefit, especially for students who learn about APIs and application developers to develop innovative applications.
Summary
The Prayer Times API is a free and simple API that provides prayer times for Islamic people. You can use this in a wide variety of applications. This article covered its available endpoints and provided sample code snippets in Python, PHP, Ruby, and Javascript to show how to use them in an application.
Leave a Reply