liveBook · Manning

Dad Jokes API

As explained in Build Python Web Apps with Streamlit

The Dad Jokes API is a web API that provides dad jokes in response to HTTP requests.

The Dad Jokes API provides random dad jokes through HTTP requests. Its endpoint is /v1/dadjokes on api.api-ninjas.com, and requests require an API key in the X-Api-Key header. section 5.3.1

A practical integration follows these steps:

  • Create an API Ninjas account, retrieve an API key, and check the API documentation. section 5.3.1
  • Call the endpoint with Python requests and test the response status before processing its contents. section 5.3.3
  • Parse the JSON response, select the first returned joke, and display it in the application. section 5.3.3
  • Use a fallback joke when the API request fails, and store the generated joke in session state to avoid regenerating it. section 5.3.3

A reusable joke function

Keep the endpoint in a configurable constant, send the API key through request headers, accept only a successful response, and return a default joke otherwise.

https://api.api-ninjas.com/v1/dadjokes

Use the endpoint listing to recognize the complete URL used for Dad Jokes API requests. section 5.3.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains account setup, authenticated requests, status checks, JSON parsing, fallback behavior, and session-state display.
Examples
Uses a jokes.py module, a random-joke endpoint, a sample JSON response, a fallback joke, and a to-do app display.
What it assumes
Assumes basic HTTP vocabulary and Python familiarity while explaining API keys, response codes, and JSON parsing.
What you can do after
Build it from scratch with Python requests and display a fetched or fallback joke in an application.
How it connects
Connects HTTP requests, API keys, JSON, status codes, error handling, and session state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.