Introduction to Wit.ai Public API

Wit.ai is a Natural Language Processing (NLP) platform that enables developers to easily build chatbots and voice assistants. The platform provides a range of API endpoints that can be used to perform tasks such as extracting meaning from text, integrating with messaging platforms, and managing user data.

In this blog post, we will explore some of the key features of the Wit.ai Public API and provide example code in JavaScript for each endpoint.

Getting Started with Wit.ai Public API

Before we can start using the Wit.ai Public API, we need to create an account and obtain an API key. Once we have our API key, we can use it to authenticate our requests to the Wit.ai API.

Here is an example of how to authenticate using the Wit.ai Public API in JavaScript:

const fetch = require("node-fetch");

const API_KEY = "INSERT_YOUR_API_KEY_HERE";

async function callWitAPI(path, method, body) {
  const url = `https://api.wit.ai/${path}`;
  const headers = {
    Authorization: `Bearer ${API_KEY}`,
    "Content-Type": "application/json",
  };

  const response = await fetch(url, {
    method: method,
    body: body ? JSON.stringify(body) : undefined,
    headers: headers,
  });

  const data = await response.json();
  return data;
}

In this example, we are using the node-fetch library to make HTTP requests. We define a function called callWitAPI that takes three parameters: path, method, and body. The path parameter specifies the endpoint URL, the method parameter specifies the HTTP method (e.g. GET, POST, etc.), and the body parameter specifies the request body (if any).

We authenticate our requests by setting the Authorization header to Bearer <API_KEY>. We also set the Content-Type header to indicate that we are sending JSON data.

Extracting Meaning from Text

The Wit.ai platform uses machine learning algorithms to extract meaning from text. We can use the /message endpoint to send a user's message to Wit.ai and receive a JSON response containing the extracted meaning.

Here is an example of how to extract meaning from text using the Wit.ai Public API in JavaScript:

async function sendMessage(message) {
  const path = "message";
  const method = "POST";
  const body = { q: message };

  const data = await callWitAPI(path, method, body);
  return data;
}

In this example, we define a function called sendMessage that takes a message parameter. We create a JSON object called body that contains the user's message. We then call the callWitAPI function with the message endpoint URL, the HTTP method, and the request body.

The callWitAPI function will return a JSON response containing the extracted meaning. We can use this data to build our chatbot or voice assistant.

Integrating with Messaging Platforms

The Wit.ai platform supports a range of messaging platforms, including Facebook Messenger, Slack, and Twilio. We can use the /integrations endpoint to manage our integration settings and receive incoming messages from these platforms.

Here is an example of how to integrate with Facebook Messenger using the Wit.ai Public API in JavaScript:

async function handleFBMessage(req, res) {
  const { message } = req.body;

  const path = "integrations/facebook/receive";
  const method = "POST";
  const body = {
    text: message.text,
    sender_id: message.sender.id,
  };

  const data = await callWitAPI(path, method, body);
  // Handle the extracted meaning here
}

In this example, we define an HTTP endpoint in our Node.js application to receive incoming messages from Facebook Messenger. We extract the text of the message and the sender's ID from the request body.

We then create a JSON object called body that contains the extracted message and sender ID, and call the callWitAPI function with the facebook/receive endpoint URL, the HTTP method, and the request body.

The data variable will contain the extracted meaning, which we can use to craft a response back to the user.

Managing User Data

The Wit.ai platform allows us to store and manage user data, such as user ID and preferences. We can use the /entities and /values endpoints to create and manage user data for our chatbot or voice assistant.

Here is an example of how to create user data using the Wit.ai Public API in JavaScript:

async function createUser(name) {
  const path = "entities/user_data/values";
  const method = "POST";
  const body = { value: name, expressions: [name] };

  const data = await callWitAPI(path, method, body);
  return data;
}

In this example, we define a function called createUser that takes a name parameter. We create a JSON object called body that contains the user's name and some example expressions.

We then call the callWitAPI function with the user_data/values endpoint URL, the HTTP method, and the request body. The Wit.ai platform will create a new user data entity with the specified name and expressions.

Conclusion

In this blog post, we have explored some of the key features of the Wit.ai Public API and provided example code in JavaScript for each endpoint. With the Wit.ai platform, developers can easily build powerful chatbots and voice assistants that can understand and respond to natural language.

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf