# Load the libraries
import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/SoilDataPoint?long="+longi+"&lat="+lat
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request
= requests.post(url,
response =headers,
headers=HTTPBasicAuth(USEREMAIL, APIKEY))
auth
# Convert from response to json
= response.json()
data
# Convert from json to pandas
= pd.DataFrame(data)
df
# Let's see the first 5 agricultural products
df.head()
Soil data
In this part we will focus on the endpoint “AFS/SoilDataPoint” endpoint, which allow the user to get data related to the soil.
Python
In this example we will test the SoilDataPoint which allow the user to get data related to the soil.
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
With your information and let’s try out the API.
R
In this example we will test the SoilDataPoint which allow the user to get data related to the soil.
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
With your information and let’s try out the API.
# import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat
# Define the url of the API
<- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/SoilDataPoint?long=", longi, "&lat=",lat)
api_url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Get the start time to API Call
tic()
# Make the POST request
<- POST(
r
api_url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
1.69 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Print out the list of the Phytofarmaceutical products
<- httr::content(r, as = "text", type = "application/json", encoding="UTF-8")
cont <-jsonlite::fromJSON(cont) %>% as.data.frame
contgt(cont)
Argilla | Sabbia | Limo | Bulk_density | Azoto | Sostanza_organica | pH |
---|---|---|---|---|---|---|
26 | 35 | 40 | 1.3 | 2.4 | 34.6 | 7 |
Node.js
In this example we will test the SoilDataPoint which allow the user to get data related to the soil.
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
With your information and let’s try out the API.
// load libraries
const axios = require('axios');
// Set username e apikey
const username = 'XXXXXXXXXXXXXX';
const password = 'XXXXXXXXXXXXXX';
// set the apiurl
const apiurl='https://www.api.automaticfarmsolutionwebapp.com/AFS/SoilDataPoint?long=';
// set the product_name parameter
const longi="13.3123";
const lat="42.4560";
// set the apiendpoint
const url = apiurl.concat(longi, "&lat=", lat)
const options = {
method: 'post',
url: url,
auth: {
username: username,
password: password,
,
};
}
axios(options)
.then(response => {
console.log('Risposta JSON:', response.data);
}).catch(error => {
console.error('Errore:', error.message);
; })
Easy - Fast - Customizable