Chuck Norris is an American martial artist and an action movie star who became an internet phenomenon over time. The ‘Chuck Norris Facts’ are jokes about Chuck Norris that stemmed from his popularity and then started to gain widespread popularity. Furthermore, these Chuck Norris facts are satirical factoids that often describe his toughness, super-human nature, attitude, masculinity, and ability to complete seemingly impossible tasks.
Currently, Chuck Norris facts have been translated into many languages and have a variety of applications. They are used in social media memes, mobile applications, joke websites, and many other non-humor websites to attract users. If you want to build an application that generates humor by including Chuck Norris jokes, using an API that retrieves Chuck Norris facts would be the ideal solution. The Chuck Norris API provided by RapidAPI makes it possible.
What is the Chuck Norris API?
Chuck Norris API is a simple and easy-to-use JSON API from chucknorris.io that enables retrieval of hand-curated Chuck Norris facts. Through this API, you can get a list of Chuck Norris jokes for a particular keyword, a list of available joke categories, or a random joke in JSON format.
Chuck Norris API is completely free, and it supports a variety of programming languages such as Python, PHP, Ruby, NodeJS, and Javascript. In this article, we will see how to use this API with multiple programming languages and SDK.
How does the Chuck Norris API work?
Chuck Norris API works using a simple API logic in which a request is sent to a specific endpoint and obtains the necessary output as the response. When sending the request, it includes ‘x-RapidAPI-key’ and ‘x-RapidAPI-host’ as authentication parameters so that the server can verify its validity. The body of the API request contains the parameters required to process it. Once the API server has received the request, the back-end application will process it. Then it will retrieve a set of jokes from the database, formulate the response, and send it to the client in JSON format.
Target Audience for the Chuck Norris API
Entertainment Application Developers
People seek websites or applications that bring them random humor for stress management and everyday motivation. The Chuck Jokes application is one such example. Developers aiming to build entertainment applications or websites can easily integrate this API to incorporate jokes to make users smile and promote regular user engagement. The Chuck Norris API is free and can be integrated into an application quickly using minimum code. It has widespread usage due to its support for multiple programming languages.
Meme Designers
Memes are a popular form of entertainment on social media that contain funny images and quotes. Memes related to Chuck Norris are also prevalent among the community. If you are a meme designer, you can use Chuck Norris facts to encourage user engagement with your memes. Moreover, you can easily integrate the Chuck Norris API into your preferred application to automate meme generation. You can also use this API to retrieve random facts and use those facts with a meme API to come up with more creative memes.
Game Developers
Since Chuck Norris is an internet sensation, there are games built around his facts, like Nonstop Chuck Norris. The Chuck Norris API helps game developers to build simple action games using Chuck Norris jokes targeting kids and families.
How to connect to the Chuck Norris API Tutorial – Step by Step
Step 1 – Sign up and Get a RapidAPI Account.
You can use RapidAPI to search and connect to thousands of APIs using a single SDK, API key, and Dashboard. It is the world’s largest API marketplace used by more than a million developers worldwide.
To create a RapidAPI account, go to rapidapi.com and click on the Sign Up icon. You can use your Google, Github, or Facebook account for Single Sign-on (SSO) or create an account manually.
Step 2 – Search the API Marketplace
Next, navigate to the Marketplace and search for “Chuck Norris API.” Then, select the ‘Chuck Norris API” from the search results.
Step 3 – Test the API
After loading the API page, you should run a test for its endpoints. For that, navigate to the ‘Endpoints’ section of the API page, which shows all the available endpoints.
Next, select the endpoint you want to test and add the required values for the parameters. Each API endpoint has code snippets in multiple languages, and it automatically adds the API keys, host, and accept parameters.
Finally, click on the “Test Endpoint” Button to test the API. You will get the response with a 200 status code upon successful testing of an API endpoint. On the other hand, it will output an error code and message if there is an error in the request.
Chuck Norris API Endpoints
Currently, RapidAPI provides three endpoints for Chuck Norris API, and below we have discussed each of them in detail.
/Jokes/Search
With this API endpoint, you can search for available Chuck Norris jokes using a specific search term or name of a joke category. This endpoint is accessible using the GET request method. It is mandatory to define the search term in the “query” parameter when sending the request. The response will include the “results” object containing an array of JSON objects having the search term. The joke resides in the “value” parameter of each JSON object. Besides, every joke has a unique id. The following is an example response you get when you use “food” as the search query. You can only use one query word for each API call.
/Jokes/Categories
This endpoint is accessible via the GET request method. Using this endpoint, you can get a list of all the available joke categories. The request will output an array of joke categories in alphabetical order. Currently, it shows you 16 Joke categories. You can use this as a reference if you want to retrieve jokes related to a particular topic. The response for the endpoint will look something as below.
/Jokes/Random
This endpoint uses the GET request method and retrieves a random Chuck Norris joke in JSON format. The response also indicates the categories that the joke belongs to. However, you can explicitly specify the category in the “category” parameter if you want to get a random joke only from a specific joke category. The following example shows a random joke returned by this endpoint for the “celebrity” category at two different times.
Integrating the Chuck Norris API to an Application
In this section, we will see how to integrate the Chuck Norris API into a software application using various programming languages such as Python, PHP, Ruby, and Javascript. The example code snippets provided here are taken from the API website, and we will be using the “search” endpoint of the API throughout all the code snippets.
In each code, you need to define values for the ‘X-RapidAPI-key,’ ‘X-RapidAPI-host,’ and ‘accept’ parameters and include them in the request header. The “accept” parameter should always be ‘application/json’ since it has been built to return the response in the JSON format. You can get these values from the API website.
Python Code Snippet (requests)
When it comes to integrating Chuck Norris API with Python, first, you should install the ‘requests’ Python module on your computer. The following python code uses the “requests” library to send a GET request to the endpoint. Define the mandatory search term in the “querystring” variable. Then use the url, header, and the querystring in the request method of the request object to send the API call programmatically. You can also use requests.post method for this purpose.
import requests url = "https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search" querystring = {"query":"food"} headers = { 'accept': "application/json", 'x-rapidapi-key': "cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5", 'x-rapidapi-host': "matchilling-chuck-norris-jokes-v1.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
PHP Code Snippet (HTTP v2)
To integrate using PHP, first, you should create two class objects for the client and the request. Then you have to set the API URL, the request method, query string containing the search keyword, and the header separately from the request object. Finally, send the request through the send method of the client object. You can also use the code snippets of CURL, HTTP V1, and Unirest/Request module provided on the API website.
<?php $client = new http\Client; $request = new http\Client\Request; $request->setRequestUrl('https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search'); $request->setRequestMethod('GET'); $request->setQuery(new http\QueryString([ 'query' => 'food' ])); $request->setHeaders([ 'accept' => 'application/json', 'x-rapidapi-key' => 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host' => 'matchilling-chuck-norris-jokes-v1.p.rapidapi.com' ]); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
Ruby (net::http)
If you are using Rubys’ net::http module for the integration, import openssl and uri modules in the beginning. In this instance, bypass the SSL certification verification by setting it to ‘VERIFY_NONE.’ Unlike in the above code examples, here, you can directly set the search keyword in the query string along with the URL.
require 'uri' require 'net/http' require 'openssl' url = URI("https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search?query=food") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["accept"] = 'application/json' request["x-rapidapi-key"] = 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' request["x-rapidapi-host"] = 'matchilling-chuck-norris-jokes-v1.p.rapidapi.com' response = http.request(request) puts response.read_body
Javascript (Axios)
Integrating this API into a Javascript application is fairly easy and requires only a few lines of code. The following code snippet shows how to call this API using the ‘axios’ module of JavaScript. You can define the parameters, url, headers, and the request method in one javascript object and use axios.request method to send the request and retrieve the response.
import axios from "axios"; const options = { method: 'GET', url: 'https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search', params: {query: 'food'}, headers: { accept: 'application/json', 'x-rapidapi-key': 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host': 'matchilling-chuck-norris-jokes-v1.p.rapidapi.com' } }; axios.request(options).then(function (response) { console.log(response.data); }).catch(function (error) { console.error(error); });
Alternatives to the Chuck Norris API
The Chuck Norris API belongs to the joke API category. So, the following are similar joke APIs available on the RapidAPI site if you are looking for some alternatives to it.
- WebKnox Jokes API – This API allows you to access over 30,000 jokes, including Chuck Norris jokes and knock-knock puns. It enables you to set different options when making requests, such as the number of jokes, the maximum length of a joke, and the minimum rating.
- Joke API V2 – This API can provide you with well-formatted jokes in JSON, XML, YAML, or plain text format. Furthermore, it consists of several filtering parameters. You can also submit jokes using this API.
- Dad Jokes API – This is another popular joke API to retrieve random and endless dad jokes. It allows you to search for a joke by an id, type, or search term.
- Jokes API – This is a full-featured joke API built on top of an extensive data set of jokes. It provides features like the joke of the day and even allows to search for a joke using the joke id or category.
- Random Stuff API – This is a freemium API that provides you with AI-based responses, random jokes, memes, images, and much more.
Benefits of the Chuck Norris API
Easy to Integrate and Use
As mentioned previously, the Chuck Norris API can be easily integrated into an application using just a few lines of code. Besides, it only requires a few parameters to process the request eliminating the need to spend a lot of time writing complicated code. You can find example code snippets on the API usage in many programming languages with two or more language frameworks or modules. Due to this simplicity, even someone with little to no background in APIs can master this API fairly quickly.
Search Options
Some Joke APIs only offer random jokes while Chuck Norris API grants appropriate endpoints to find jokes that contain a specific keyword and jokes related to a particular topic to provide topical humor. Moreover, this API consists of a significant number of joke categories. These options make Chuck Norris API ideal for several types of joke applications such as real-time random joke generators, joke finders, etc.
Free API
Chuck Norris API allows developers to enjoy all its capabilities free of charge. This is a huge benefit, especially for students who learn about APIs and application developers who use this API for innovation purposes.
Summary
Chuck Norris API is a simple yet powerful Chuck Norris jokes generation API with many applications and benefits. In this article, we discussed the available endpoints, target audience, benefits of this API, and a few alternatives to it. Further, we provided sample code snippets in Python, PHP, Ruby, and Javascript to show how to integrate and use this API in an application.
Leave a Reply