FruitKount - Getting Started

Welcome to the FruitKount API Documentation.

This API is the cloud service that allow to access the acquired data of our IoT system based on artificial intelligence that count each individual production unit (bunch, apple, peach, pear) and also determines its weight and its relative position.

If you would like to learn more, please visit our website

We developed this API with the following principles:

  • Easy to use

  • Safe

  • Ready to be integrated with third-party platforms

We at Automatic Farm Solution, we believe in collaboration, and we are ready to support you in order to Test / Integrate our API.

If you need any help/support please don’t hesitate to send us an email to info@automaticfarmsolution.com. One operator will help you as soon as we can.

All the API Documentation will be provided in three different programming languages which are:

  1. Python
  2. R
  3. Node.js

Let’s Get Started

In order to start to use our API:

  • you have to register to our App, please to go Register Page in order to register. After the registration you should receive an email in order to confirm your detail. After this you are ready to go.

  • moreover you should have a sensors linked to your account. If you don’t have any sensors and you would like to test out the system, please send us an email to info@automaticfarmsolution.com by asking to add one of our sensor

I will now show you how to:

  1. Authenticate

  2. Get your account information

Authentication

Our API follows the JSON Web Token in order to authenticate.

So before to make any requests, you should first get the JSON Web Token and then set it as Bearer Authentication Code.

In order to get the Bearer Authentication Code you have to login first. In order to use the authentication/login/ endpoint you need to provide as body of the POST request these elements:

  1. Email
  2. Password

After getting the Bearer authentication code you have to pass it each time that you are making a request to our API

Python

import requests

# Set the email and password
email = "email"
password = "password"

# Set the domain
domain = "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint = "authentication/token/"

# Create the API URL
api_url = domain + endpoint

# Create the JSON body element
body = {
    "email": email,
    "password": password
}

# Set headers for the request
headers = {
    "Content-Type": "application/json"
}

# Make the POST request
response = requests.post(api_url, json=body, headers=headers)

# Check the status code
print(f"Status Code: {response.status_code}")

if response.status_code == 200:
    # Convert the response to JSON
    response_json = response.json()
    
    # Print out the token key item
    token = response_json.get("token", None)
    if token:
        print(f"Token: {token}")
    else:
        print("Token not found in the response.")
else:
    print(f"Error: {response.text}")

R

library(httr)
library(tidyverse)

# Set the email and password
email <- Sys.getenv("useremail")
password <- Sys.getenv("password")

# Set the domain
domain <- "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint <- "authentication/token/"

# Create the API URL
api_url <- paste0(domain, endpoint)

# Create the JSON body with email and password
json_body <- list(email = email, 
                  password = password)

# Make the POST request
response <- POST(
  api_url,
  body = json_body,
  encode = "json", # Ensures the body is sent as JSON
  content_type_json() # Sets the Content-Type header to application/json
)

# Check the status code of the response
print(response$status_code)
[1] 200
# Check the content
cont <- httr::content(response, as = "text", type = "application/json", encoding="UTF-8")
cont<-jsonlite::fromJSON(cont) %>% as.data.frame

# The following is the Bearer code that you have to use for each request 
# print(cont$token)

Node.js

const axios = require('axios');
const fs = require('fs');
const path = require('path'); // For cross-platform file path handling

// Set user email and password
const useremail = 'useremail';
const password = 'password';

// Set API base URL and endpoint
const apiUrl = 'https://fruitkountbackend.automaticfarmsolutionwebapp.com/';
const endpoint = 'authentication/token/';
const apiEndpoint = apiUrl.concat(endpoint);

// Payload for the POST request
const payload = {
  email: useremail,
  password: password,
};

// Output file path
const outputFilePath = path.resolve(__dirname, 'output.json'); // Resolve to current directory

// Function to make the POST request
async function makePostRequest() {
  try {
    const response = await axios.post(apiEndpoint, payload, {
      headers: {
        'Content-Type': 'application/json',
      },
      responseType: 'json',
    });

    // Print the token key item from the JSON response
    if (response.data && response.data.token) {
      console.log('Token:', response.data.token);
    } else {
      console.log('Token not found in response data.');
    }

    // Write response data to a file
    fs.writeFileSync(outputFilePath, JSON.stringify(response.data, null, 2), 'utf-8');
    console.log('Response data saved to:', outputFilePath);
  } catch (error) {
    console.error('An error occurred:', error.message);
    if (error.response) {
      console.error('Response status:', error.response.status);
      console.error('Response data:', error.response.data);
    }
  }
}

// Execute the function
makePostRequest();

User Details

Now let’s get your account information in order to check if you are well setted to make your first API request.

If the response give to you a status code as 200 - You are ready to make your first API Request

If the response give to you a status code different than 200 - You are NOT ready to make your first API Request - If you need help or support please send us an email to info@automaticfarmsolution.com

Python

import os
import requests
import pandas as pd

# Set the token from the environment variable
token = "yourtoken"

# Set the domain
domain = "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint = "authentication/user-detail/"

# Create the API URL
api_url = f"{domain}{endpoint}"

# Make the GET request with Bearer Authentication
headers = {
    "Authorization": f"Bearer {token}"
}

response = requests.get(api_url, headers=headers)

# Check the status code of the response
print(response.status_code)

# Check the content of the response
if response.status_code == 200:
    cont = response.json()  # Parse JSON response into a Python dict

    print(cont)
else:
    print("Failed to retrieve data. Status code:", response.status_code)

R

library(httr)
library(tidyverse)

# Set the token from the environment variable
token <- Sys.getenv("token")

# Set the domain
domain <- "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint <- "authentication/user-detail/"

# Create the API URL
api_url <- paste0(domain, endpoint)

# Make the GET request with Bearer Authentication
response <- GET(
  api_url,
  add_headers(Authorization = paste("Bearer", token))
)

# Check the status code of the response
print(response$status_code)
[1] 200
# Check the content of the response
cont <- httr::content(response, as = "text", type = "application/json", encoding = "UTF-8")
cont <- jsonlite::fromJSON(cont) %>% as.data.frame()

# Print out the user details
# print(cont)

Node.js

const axios = require('axios');
const fs = require('fs');
const path = require('path'); // For cross-platform file path handling

// Set user token
const token = 'yourtoken';

// Set API base URL and endpoint
const apiUrl = 'https://fruitkountbackend.automaticfarmsolutionwebapp.com/';
const endpoint = 'authentication/user-detail/';
const apiEndpoint = apiUrl.concat(endpoint);

// Output file path
const outputFilePath = path.resolve(__dirname, 'output.json'); // Resolve to current directory

// Function to make the GET request
async function makeGetRequest() {
  try {
    const response = await axios.get(apiEndpoint, {
      headers: {
        'Authorization': `Bearer ${token}`,
      },
    });

    // Print the status code
    console.log('Status Code:', response.status);

    // Check if the response contains data
    if (response.data) {
      // Write response data to a file
      fs.writeFileSync(outputFilePath, JSON.stringify(response.data, null, 2), 'utf-8');
      console.log('Response data saved to:', outputFilePath);
    } else {
      console.log('No data found in the response.');
    }
  } catch (error) {
    console.error('An error occurred:', error.message);
    if (error.response) {
      console.error('Response status:', error.response.status);
      console.error('Response data:', error.response.data);
    }
  }
}

// Execute the function
makeGetRequest();
Back to top