Authorization

Authorization

Your retailer configuration includes the list of APIs and capabilities (collectively called APIs) that your organization has permission to use. When your site generates an access token to authenticate with Foodin platform, you specify which API you want to access by setting the scope. The generated access token is limited to that API.

PermissionDescriptionscopegrant_type
Fullfillment APIAccess stores, service options, reservations, order creation, and order management.connect:fulfillmentclient_credentials
Recommendations APIFind replacement items or complementary items in a backend implementation.connect:recommendationsclient_credentials
Pantry APIAccess and manage products in User pantryconnect:pantryclient_credentials
Account linksLink a customer's Partner user account to their Foodin account.account_linkingauthorization_code
Elements APIImplement Foodins Elements in your app or websiteelements.mountauthorization_code

Generate an access token

POST api.foodin.ai/oauth/token;

Returns an access token. The access token must be included in all other requests as a Bearer token for authentication purposes. Before you begin, ensure you have a client ID and secret from Foodin. You need to pass these values in the request.

Specify the scope and grant type that is required for the API you intend to use.

Request

FieldParameterDescription
client_idstringThe client ID.
client_secretstringThe client secret.
grant_typestringThe grant type.
scopestringThe APIs that this token can access. Default is all the APIs specified in the retailer application configuration.
codestringThe authorization code.
redirect_uristringThe redirect URI when the authorization code was generated.
assertionstringThe assertion.

Request examples

FieldParameterDescription
client_idstringThe client ID.
client_secretstringThe client secret.
grant_typestringThe grant type.
scopestringThe APIs that this token can access. Default is all the APIs specified in the retailer application configuration.
codestringThe authorization code.
redirect_uristringThe redirect URI when the authorization code was generated.
assertionstringThe assertion.
const axios = require("axios");
 
async function getToken() {
  try {
    const response = await axios({
      method: "post",
      url: "https://api.foodin.ai/oauth/token",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      data: {
        client_id: "string",
        client_secret: "string",
        grant_type: "string",
        scope: "string",
        code: "string",
        redirect_uri: "string",
        assertion: "string",
      },
    });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}
 
getToken();

Response

FieldParameterDescription
access_tokenstringThe token to be used to authenticate requests.
token_typestringThe token type.
expires_innumberThe number of seconds the token will expire in.
created_atnumberThe epoch time of when the token was created.
scopestringThe scope of the token.

Response example

{
  "access_token": "mhtEdMZYPypuW_I0fYken8cAqE7llDaoNefHSeVj9u4",
  "token_type": "Bearer",
  "expires_in": 86400,
  "scope": "connect:fulfillment",
  "created_at": 1603897760
}

Authentication errors

HTTP CodeCauseDescription
400Invalid authorization code or redirect URI"Assertion is not provided or invalid assertion provided for the grant_type."
401Invalid client ID or secret"Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method."

Revoke an access token

POST api.foodin.ai/oauth/token/revoke

Revokes any access token. You need to include your client ID and secret and the access token in the request body.

FieldTypeDescription
client_idstringThe client ID.
client_secretstringThe client secret.
const axios = require("axios");
 
async function revokeToken() {
  try {
    const response = await axios({
      method: "post",
      url: "https://api.foodin.ai/oauth/token/revoke",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      data: {
        client_id: "string",
        client_secret: "string",
        token: "string",
      },
    });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}
 
revokeToken();

Response examples

200 success
{
  // Empty
}
HTTP CodeCauseDescription
403Unauthorized client."You are not authorized to revoke this token"

Authentication for event callbacks

To enable event callbacks to be sent to your retailer site, OAuth v2.0 must be enabled on the callback endpoint. For information about OAuth 2.0, see the OAuth 2.0 Authorization Framework (RFC).

OAuth 2.0 details required Foodin requires the following configuration details to support callbacks:

  • An endpoint to use for Connect callbacks. The endpoint must be protected by OAuth 2.0.
  • An endpoint to use for OAuth 2.0 authentication, which accepts a form POST with content type application/x-www-form-urlencoded and the following query parameters:
ParameterDescription
client_idThe ID that Connect uses to authenticate with your site.
client_secretThe secret that Connect uses to authenticate with your site.
grant_typeThe grant type for the token. Always client_credentials.
scopeIf your OAuth 2.0 configuration includes a scope for Connect callbacks, the scope must be included as a parameter.

Response requirements

ParameterDescription
access_tokenThe access token that Connect sends with callbacks.
expires_inWhen the token expires. Optionally, you can configure a fixed expiry time with Foodin.

Process

Foodin Connect requests an access token to begin sending callback events. When the access token expires, Connect requests a new token.

  • Connect uses the OAuth 2.0 endpoint to send a request for an access token.
  • The retailer site returns an access token.
  • Connect uses the callback endpoint to send the callback along with the access token.
  • The retailer site authenticates the request and permits the callback.