API Documentation

Rice Nutrition API Documentation

Welcome to the Rice Nutrition API documentation. This guide will help you get started with using our API to access rice nutrition data.

Table of Contents

Create an app

An app provides the Client ID and Client Secret needed to request an access token by implementing any of the authorization flows. To create an app, go to your dashboard, click on the Create an app button and enter the following information:

  • App Name: My App
  • App Description: This is my first app
  • Redirect URI: You won't need this parameter in this example, so let's use http://localhost:3000.

Request an access token

The access token is a string which contains the credentials and permissions that can be used to access a given resource (e.g ricenutrition, ricevarieties).

In order to request the access token you need to get your Client_ID and Client Secret:

  • Go to the Dashboard
  • Click on the name of the app you have just created (My App)
  • Click on the Settings button

The Client ID can be found here. The Client Secret can be found behind the View client secret link.

With our credentials in hand, we are ready to request an access token. This tutorial uses the Client Credentials, so we must:

  • Send a POST request to the token endpoint URI.
  • Add the Content-Type header set to the application/x-www-form-urlencoded value.
  • Add a HTTP body containing the Client ID and Client Secret, along with the grant_type parameter set to client_credentials.
curl -X POST "https://sso-nutri.ricethailand.go.th/realms/myrealm/protocol/openid-connect/token" \
   -H "Content-Type: application/x-www-form-urlencoded" \
   -d "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret"

The response will return an access token valid for 1 hour:

{
"access_token": "BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11",
"token_type": "Bearer",
"expires_in": 3600
}

Request rice variety data

For this example, we will use the Get Rice variety endpoint to request information about an rice variety. According to the API Reference, the endpoint needs the ID of the variety.

curl "http://localhost:3000/api/rice/nutrition/672492543a7a7a8a0fb4a08d" \
   -H "Authorization: Bearer BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11"

if everything goes well, the API will return the following JSON response:

{
  "riceVarieties": {
      "th": "เขียวใหญ่",
      "en": "Khiao Yai"
  },
  "cropSampleID": 3,
  "variety": "6723afa27afca63cb95c5731",
  "cropSampleName": "เขียวใหญ่",
  "dataSource": "การประชุมวิชาการข้าวและธัญพืชเมืองหนาว ครั้งที่ 34 พ.ศ.2560",
  "nutritionData": [],
  "href": "http://localhost:3000/api/rice/nutrition/672492543a7a7a8a0fb4a08d",
  "id": "672492543a7a7a8a0fb4a08d"
}

Next Steps

  1. Dashboard Create an app
  2. The authorization guide provides detailed information about which authorization flow suits you best. Make sure you read it first!
  3. Check out our API Reference