Everyone loves humor as it helps to kill boredom. Funny apps are a popular form of entertainment that brings humor. Almost every smartphone user has an application that contains hilarious content such as jokes, memes, funny videos, and pictures. Among those hilarious content lies dad jokes or jokes by fathers. A dad joke is typically a short and sweet, one-line joke presented as a question and a humorous answer given by fathers among their families. The purpose of dad jokes is to either make kids laugh or arouse adverse reactions. Dad jokes are widely used in social media posts and memes.
There is a lot of joke APIs for different types of jokes. Anyone who is looking to build an application or website with funny content can make use of them. The Dad Jokes API from RapidAPI is one of the best APIs you can use to incorporate some traditional sense of humor from dads.
What is Dad Jokes API?
Dad Jokes API is a simple and easy-to-use JSON API based on DadJokes.io that provides unlimited dad jokes. Using this API, you can retrieve random dad jokes and search for a joke by its id, type, or a particular term in JSON format.
This API includes affordable pricing plans as well as a free subscription. Moreover, it supports multiple programming languages like Python, PHP, Ruby, and Javascript. This tutorial will help you learn more about this API and show you how to use it with different programming languages.
How does the Dad Jokes API work?
The functionality of Dad Jokes API is based on a simple REST API logic. When you call a specific endpoint of this API, the corresponding server will receive the request. Then the backend server will process the request and send back the necessary output to you as the response. The authentication parameters’ x-RapidAPI-key’ and ‘x-RapidAPI-host’ are included in the request header and used to validate the authenticity of the request. The request body contains the parameters required to process the request. Once the API server has received the request, its backend application will process it. The application will retrieve one or more dad jokes based on the specified criteria from a database and formulate the response. Finally, the server sends the response to the client in JSON format.
Target Audience for the Dad Jokes API
Entertainment Application Developers
Entertainment applications and websites with funny content like BuzzFeed and 9GaG have a great demand as they offer stress relief and everyday motivation for users. Developers aiming to build such entertainment applications or websites can integrate this API to incorporate dad jokes to promote regular user engagement and make users smile. Furthermore, this API will be a perfect choice if you are specifically targeting kids.The Dad Jokes API is affordable and can be quickly integrated into an application using just a few lines of code. It has widespread usage due to its support for multiple programming languages.
Meme Designers
Memes are popular media for entertainment that contain funny images and quotes. Memes related to dad jokes are also widespread on social media. The random dad jokes returned from this API can be used with meme APIs to design more creative memes. So if you are a meme designer, you can easily integrate this API into an application of your choice and automate the meme generation.
How to connect to the Dad Jokes API Tutorial – Step by Step Guide
Step 1 – Sign up and Get a RapidAPI Account.
RapidAPI is the world’s largest API marketplace used by more than a million developers worldwide. You can use RapidAPI to search and connect to thousands of APIs using a single SDK, API key, and Dashboard.
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 manually create an account.
Step 2 – Search the API Marketplace
Next, Navigate to the Marketplace and search for “Dad Jokes API”. Then, select the ‘Dad Jokes API” from the search results as shown below.
Step 3 – Subscribe to the API
We need to subscribe to the Dad Jokes API to use it. Navigate to the “pricing” section of the API and subscribe to your preferred plan. For this article, we select the free basic plan of the API. Then, click on the “subscribe” button to activate the subscription.
Step 4 – Test the API
After loading the API page, you can run a test for the endpoints. For that, navigate to the ‘Endpoints’ section of the API page, which shows all the available endpoints. Then 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 and host 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 a message if there is an error in the request.
Dad Jokes API Endpoints
RapidAPI provides three endpoints for Dad Jokes API, which are accessible using the GET request method.
Random Jokes
This API endpoint allows you to retrieve a random dad joke. The response body includes the joke object with the id, joke type, setup, and punchline. The ‘setup’ is the question part of the joke, while the ‘punchline’ is the hilarious answer to that question. Furthermore, each joke object contains a unique string id. The following example shows you a random dad joke returned by this endpoint.
JokeByID
You can use this endpoint to retrieve a specific joke using its id. For that, you have to define the joke id in the mandatory ‘id’ parameter. The following is an example of a dad joke returned by this endpoint for a specific identifier.
JokeByType
This endpoint is to retrieve a set of jokes related to a specific joke type. There, you have to define the joke type in the mandatory ‘type’ parameter. Some of the joke types supported by this endpoint include;
- general
- knock-knock
- programming
It will return you an array of joke objects related to the specific type or category you defined. The following example shows dad jokes returned by this endpoint for the type ‘programming’.
Search
This endpoint allows you to search for jokes that are available for a particular word of your choice. For that, you have to define the search term in the mandatory ‘term’ parameter. For instance, this endpoint will provide you with the following response if you searched for dad jokes about dogs.
Integrating the Dad Jokes API to an Application
This section will show you how to integrate the Dad Jokes API into a software application with Python, PHP, Ruby, and Javascript by providing example code snippets from the API site. We will be using the “GET search” endpoint of the API throughout all the code snippets.
In each code snippet, you need to define values for the ‘X-RapidAPI-key’ and ‘X-RapidAPI-host’ parameters and include them in the request header. You can get these values from the API website.
Python Code Snippet (requests)
The following code can be used to integrate the API with Python. There, we have used the “requests” library to send a GET request to the endpoint. Hence, first, you need to install that module on your computer. Then specify all the parameters in the “querystring” variable. You can send the API call using the request method of the request object. You can also use the ‘requests.post’ method for this purpose.
import requests url = "https://dad-jokes.p.rapidapi.com/joke/search" querystring = {"term":"cow"} headers = { 'x-rapidapi-key': "cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5", 'x-rapidapi-host': "dad-jokes.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
PHP Code Snippet (HTTP v2)
When it comes to integrating with PHP, first of all, create class objects for the client and the request. Then, set the request URL, request method, query string containing the search keyword, and the request header separately from the request object. Finally, send the request through the send method of the client object. You can also use cURL, HTTP V1, and Unirest/Request module code snippets provided on the API website.
<?php $client = new http\Client; $request = new http\Client\Request; $request->setRequestUrl('https://dad-jokes.p.rapidapi.com/joke/search'); $request->setRequestMethod('GET'); $request->setQuery(new http\QueryString([ 'term' => 'cow' ])); $request->setHeaders([ 'x-rapidapi-key' => 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host' => 'dad-jokes.p.rapidapi.com' ]); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
Ruby (net::http)
Suppose you are using the net::http module of Ruby, import OpenSSL and URI modules in the beginning. In this instance, bypass 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://dad-jokes.p.rapidapi.com/joke/search?term=cow") 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["x-rapidapi-key"] = 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' request["x-rapidapi-host"] = 'dad-jokes.p.rapidapi.com' response = http.request(request) puts response.read_body
Javascript (Axios)
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 the request method of Axios to send the request and retrieve the response.
import axios from "axios"; const options = { method: 'GET', url: 'https://dad-jokes.p.rapidapi.com/joke/search', params: {term: 'cow'}, headers: { 'x-rapidapi-key': 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host': 'dad-jokes.p.rapidapi.com' } }; axios.request(options).then(function (response) { console.log(response.data); }).catch(function (error) { console.error(error); });
The Dad Jokes API belongs to the joke API category. Following are the similar joke APIs to the Dad Jokes API available on the RapidAPI site.
- WebKnox Jokes API – This API allows you to access over 30,000 jokes, including Chuck Norris jokes and knock-knock puns. Moreover, it enables you to set different options when querying, such as the number of jokes, the maximum length of a joke, and the minimum rating.
- Joke API V2 – This API provides 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.
- Chuck Norris Jokes – Another free joke API that provides you jokes about the famous action movie star Chuck Norris. This API allows you to get a random Chuck joke, search for a joke using a search term and search jokes by category.
- Jokes API – This is a full-featured joke API built on top of an extensive jokes data set. It provides some special features like the joke of the day. In addition, it provides the ability to search for a joke using the joke id or category.
- Random Stuff API – This is a freemium API that offers you AI-based responses, random jokes, memes, images, and many more.
Benefits of the Dad Jokes API
Easy to Integrate
Dad Jokes API is a simple API that requires only a few parameters to process data. Due to that, you don’t have to spend a lot of time writing complicated code to integrate this API into an application. Moreover, you can find example code snippets for integration in many programming languages for two or more language frameworks or modules. This simplicity enables even someone with little to no background using APIs to master the API reasonably quickly.
Multiple Search Options
Dad Jokes API provides appropriate endpoints to find all the jokes that contain a specific keyword and search a joke by its id or type. These options make it easy to use this API in several types of joke applications such as real-time random dad joke generators, dad joke finders, etc.
Freemium API
Dad Jokes API is an affordable API that costs you only $20 to get its full capabilities. Besides, it allows developers to utilize most of its features free of charge. It will be a huge benefit, particularly for students who learn about APIs and application developers who aim to build innovative applications.
Summary
Dad Jokes API is a simple yet powerful API providing endless dad jokes that can be used to build funny applications. In this article, we discussed its available endpoints, benefits, alternatives, and target audience. We also explained how to integrate this API into an application using sample code snippets in Python, PHP, Ruby, and Javascript.
Leave a Reply