Test the API

Start trying the API

After you have set the api key you are ready to go to use our service. In the next part of the documentation you will find two different block:

  1. Python
  2. R
  3. Node.js
  4. Java

Please try out the follow examples in order to see if the connection is working.

Python

In this example we will test the AutoPrescriptionMapGEOjson endpoint which allow the user to get an automatic prescription map.

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
  5. file_path_to_geojson (you can find it here county.geojson the county file of the example)

With your information and let’s try out the API.

# Load the libraries

import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionMapGEOjsonNew?fieldnumber=1&action=none&AutoPrescription=True&NumberZone=3&userfertilizer=200&Strategy=highwherehigh&ProductType=Solid&ProductName=Urea&GeographicReference=WGS84&basemap=one&DoseDifference=0"

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey"

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Read the GeoJSON file contents

with open(file_path_to_geojson, "r") as file:
    geojson_data = file.read()

# Set the headers for the request

headers = {
    "Content-Type": "application/json"
}

# Make the POST request with the GeoJSON data as the request body

response = requests.post(url, 
                         data=geojson_data, 
                         headers=headers,
                         auth=HTTPBasicAuth(USEREMAIL, APIKEY))

b = bytes(response.content)

with fiona.BytesCollection(b) as f:
    crs = f.crs
    gdf = gpd.GeoDataFrame.from_features(f, crs=crs)

#  Visualize the data

gdf.explore("Zone")

R

Here to test the API connection you have to replace:

  1. useremail
  2. apikey
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
  5. file_path_to_geojson (you can find it here county.geojson the county file of the example)

with your account information.

# Load the libraries

library(tictoc)
library(httr)
library(geojsonio)
library(mapview)

# Define the url of the API

url <- "https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionMapGEOjsonNew?fieldnumber=1&action=none&AutoPrescription=True&NumberZone=3&userfertilizer=200&Strategy=highwherehigh&ProductType=Solid&ProductName=Urea&GeographicReference=WGS84&basemap=one&DoseDifference=0"

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey"

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Get the start time to API Call

tic()

# Make the POST request

api_call <- POST(
  url,
  httr::authenticate(
    user = Sys.getenv(USEREMAIL),
    password = Sys.getenv(APIKEY)
  ),
  body=httr::upload_file(file_path_to_geojson)
)

# Print out the seconds needed to create the prescription map

print("The API needed:")
[1] "The API needed:"
toc()
4.75 sec elapsed
# Visualize the prescription map

prscription_map <- content(api_call, as = "text", type = "application/geo+json")
prscription_map<-geojson_sp(prscription_map)
mapview(prscription_map, zcol="Zone", layer.name="Prescription Map")


And here we go in just around 11.25 seconds you have created a prescription map that you can directly insert or send to the tractor display.

Node.js

Here to test the API connection you have to replace:

  1. useremail
  2. apikey
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
  5. file_path_to_geojson (you can find it here county.geojson the county file of the example)

with your account information.

// Load Libraries

const fs = require('fs');
const axios = require('axios');

// Set the useremail & passowrd

const useremail = 'XXXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXXX';

// Set API Endpoint

const apiEndpoint = 'https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionMapGEOjsonNew?fieldnumber=1&action=none&AutoPrescription=True&NumberZone=3&userfertilizer=200&Strategy=highwherehigh&ProductType=Solid&ProductName=Urea&GeographicReference=WGS84&basemap=one&DoseDifference=0';

// Set path to load the geojson to send as body of POST request to the API

const file_path_to_geojson = './county.geojson';

// Set path to save the geojson prescription map

const outputFilePath = './result.geojson'; 

(async () => {
  try {

    const geojsonContent = await fs.promises.readFile(file_path_to_geojson, 'utf8');
    
    const geojsonObject = JSON.parse(geojsonContent);

    
    const authHeader = `Basic ${Buffer.from(`${useremail}:${apikey}`).toString('base64')}`;

    
    const response = await axios.post(apiEndpoint, geojsonObject, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': authHeader
      }
    });

    const resultGeoJSON = response.data;
    
    console.log('Answear of the API:', resultGeoJSON);

    await fs.promises.writeFile(outputFilePath, JSON.stringify(resultGeoJSON, null, 2), 'utf8');

  } catch (err) {
    console.error('Error:', err.message);
  }
})();

Java

Here to test the API connection you have to replace:

  1. useremail
  2. apikey
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
  5. file_path_to_geojson (you can find it here county.geojson the county file of the example)

with your account information.

WORK IN PROGRESS

Easy - Fast - Customizable

Back to top