Cover....1
Copyright....8
Table of Contents....11
Preface....19
Why Should You Read This Book?....19
Who This Book Is For....19
Data Scientists....19
API Developers and Designers....20
Job Seekers and Role Changers....20
Creating Portfolio Projects....20
Using This Book....21
What This Book Is Not....21
Why Fantasy Football?....22
Get More Tips on APIs, AI, and Data Science....23
Conventions Used in This Book....23
Using Code Examples....24
O’Reilly Online Learning....24
How to Contact Us....25
Acknowledgments....25
Part I. Building APIs for Data Science....27
Chapter 1. Creating APIs That Data Scientists Will Love....29
How Do Data Scientists Use APIs?....29
What Tools Do Data Scientists Use?....30
Designing APIs for Data Scientists....31
Introducing Your Part I Portfolio Project....32
Every API Has a Story....32
Meeting Your Company: SportsWorldCentral....33
SWC Needs an API....35
Selecting the First API Products....36
Identifying Potential Users....36
Creating User Stories....37
Additional Resources....39
Summary....39
Chapter 2. Selecting Your API Architecture....41
API Architectural Styles....41
Representational State Transfer (REST)....42
Graph Query Language (GraphQL)....43
gRPC....43
Your Choice: REST....44
Technology Architecture....45
Software Used in This Chapter....47
Python....47
GitHub....47
Getting Started with Your GitHub Codespace....48
Creating Your GitHub Account....48
Cloning the Part I Repository....48
Launching Your GitHub Codespace....49
Touring Your New Codespace....50
Making Your First Commit....51
Additional Resources....53
Summary....54
Chapter 3. Creating Your Database....55
Components of Your API....55
Software Used in This Chapter....56
SQLite....56
SQLAlchemy....57
pytest....57
Creating Your SQLite Database....58
Creating Database Tables....58
Understanding Table Structure....61
Loading Your Data....62
Accessing Your Data Using Python....63
Installing SQLAlchemy in Your Environment....63
Creating Python Files for Database Access....65
Creating the Database Configuration File....70
Creating SQLAlchemy Helper Functions....71
Installing pytest in Your Environment....75
Testing Your SQLAchemy Code....75
Additional Resources....78
Summary....79
Chapter 4. Developing the FastAPI Code....81
Continuing Your Portfolio Project....81
Software Used in This Chapter....82
FastAPI....82
HTTPX....83
Pydantic....83
Uvicorn....84
Copying Files from Chapter 3....84
Installing the New Libraries in Your Codespace....85
Creating Python Files for Your API....85
Creating Pydantic Schemas....85
Creating Your FastAPI Controller....90
Testing Your API....96
Launching Your API....99
Additional Resources....101
Summary....102
Chapter 5. Documenting Your API....103
Sending a Signal of Trust....103
Making Great API Docs....104
Core Features....104
Extra Features....105
Reviewing Examples of API Documentation....106
Sleeper App....106
MyFantasyLeague....107
Yahoo! Fantasy Football....109
Viewing Your API’s Built-in Documentation....109
Copying Files from Chapter 4....110
Documentation Option 1: Swagger UI....111
Documentation Option 2: Redoc....117
Working with Your OpenAPI Specification File....118
Continuing Your Portfolio Project....121
Adding Details to the OAS info Object....122
Adding Tags to Categorize Your Paths....123
Adding More Details to Individual Endpoints....123
Adding Parameter Descriptions....124
Viewing the Changes in Swagger UI....125
Regression-Testing Your API....126
Updating Your README.md....127
Additional Resources....129
Summary....130
Chapter 6. Deploying Your API to the Cloud....131
Benefits and Responsibilities of Cloud Deployment....131
Benefits....132
Responsibilities....132
Choosing a Cloud Host for Your Project....133
Setting Up Your Project Directory....134
Using GitHub Codespaces as a Cloud Host....134
Deploying to Render....135
Signing Up for Render....136
Creating a New Web Service....136
Auto-Deploying a Change to Your API....138
Shipping Your Application in a Docker Container....139
Verifying Docker Installation....140
Creating a Dockerfile....140
Creating a .dockerignore File....141
Building a Container Image....142
Running Your Container Image Locally....142
Deploying to AWS....143
Creating a Lightsail Container Service....143
Installing the AWS CLI....145
Installing the Amazon Lightsail Container Services Plug-in....145
Configuring Your Login Credentials....145
Pushing Your Container Image to Lightsail....145
Creating a Lightsail Deployment....147
Updating Your API Documentation....151
Additional Resources....151
Summary....152
Chapter 7. Batteries Included: Creating a Python SDK....153
SDKs Bridge the Gap....154
Picking a Language for Your SDK....157
Starting with a Minimum Viable SDK....158
Expert Tip: Making Your SDK Easy to Install....158
Expert Tip: Making the SDK Consistent and Idiomatic....160
Building a Feature-Rich SDK....162
Expert Tip: Using Sane Defaults....163
Expert Tip: Providing Rich Functionality....165
Expert Tip: Performing Logging....170
Expert Tip: Hiding Your API’s Complicated Details....172
Expert Tip: Supporting Bulk Downloads....174
Expert Tip: Documenting Your SDK....177
Testing Your SDK....179
Expert Tip: Supporting Every Task the API Supports....183
Completing Your Part I Portfolio Project....184
Additional Resources....186
Summary....187
Part II. Using APIs in Your Data Science Project....189
Chapter 8. What Data Scientists Should Know About APIs....191
Using a Variety of API Styles....191
HTTP Basics....193
How to Consume APIs Responsibly....195
Separation of Concerns: Using SDKs or Creating API Clients....196
How to Build APIs....198
How to Test APIs....198
API Deployment and Containerization....199
Using Version Control....199
Introducing Your Part II Portfolio Project....200
Getting Started with Your GitHub Codespace....200
Cloning the Part II Repository....200
Launching Your GitHub Codespace....201
Running the SportsWorldCentral (SWC) API Locally....202
Additional Resources....203
Summary....204
Chapter 9. Using APIs for Data Analytics....205
Custom Metrics for Sports Analytics....205
Using APIs as Data Sources for Fantasy Custom Metrics....206
Creating a Custom Metric: The Shark League Score....208
Software Used in This Chapter....209
httpx....209
Jupyter Notebooks....209
pandas....210
Installing the New Libraries in Your Codespace....210
Launching Your API in Codespaces....210
Creating an API Client File....211
Creating Your Jupyter Notebook....212
Adding General Configuration to Your Notebook....214
Working with Your API Data....215
Calculating the League Balance Score....218
Calculating the League Juice Score....219
Creating the Shark League Score....221
Additional Resources....222
Summary....222
Chapter 10. Using APIs in Data Pipelines....223
Types of Data Sources for Data Pipelines....224
Planning Your Data Pipeline....224
Orchestrating the Data Pipeline with Apache Airflow....225
Installing Apache Airflow in GitHub Codespaces....226
Creating Your Local Analytics Database....230
Launching Your API in Codespaces....231
Configuring Airflow Connections....231
Creating Your First DAG....232
Coding a Shared Function....235
Running Your DAG....237
Summary....239
Chapter 11. Using APIs in Streamlit Data Apps....241
Engaging Users with Interactive Visualizations....241
Software Used in This Chapter....242
nfl_data_py....243
Streamlit....243
Installing Streamlit and nfl_data_py....243
Launching Your API in Codespaces....243
Reusing the Chapter 9 API Client File....244
Creating Your Streamlit App....244
Updating the Entrypoint File....245
Running Your Streamlit App....246
Creating the Team Rosters Page....247
Creating the Team Stats Page....250
Deploying Your Streamlit App....254
Completing Your Part II Portfolio Project....254
Additional Resources....255
Summary....256
Part III. Using APIs with Artificial Intelligence....257
Chapter 12. Using APIs with Artificial Intelligence....259
The Overlap of AI and APIs....259
Designing APIs to Use with Generative AI and LLMs....261
Defining Artificial Intelligence....263
Generative AI and Large Language Models (LLMs)....264
Creating Agentic AI Applications....264
Introducing Your Part III Portfolio Project....266
Getting Started with Your GitHub Codespace....266
Cloning the Part III Repository....266
Launching Your GitHub Codespace....267
Additional Resources....268
Summary....268
Chapter 13. Deploying a Machine Learning API....269
Training Machine Learning Models....270
New Software Used in This Chapter....272
ONNX Runtime....272
scikit-learn....272
sklearn-onnx....272
Installing the New Libraries in Your Codespace....273
Using the CRISP-DM Process....273
Business Understanding....274
Data Understanding....275
Data Preparation....277
Modeling....277
Evaluation....280
Deployment....280
Additional Resources....289
Summary....289
Chapter 14. Using APIs with LangChain....291
Calling AI Using APIs (via LangChain)....292
Creating a LangGraph Agent....293
Signing Up for Anthropic....294
Launching Your GitHub Codespace....295
Installing the New Libraries in Your Codespace....296
Creating Your Jupyter Notebook....296
Chatting with the LangGraph Agent....299
Running the SportsWorldCentral (SWC) API Locally....301
Installing the swcpy Software Development Kit (SDK)....302
Creating a LangChain Toolkit....302
Calling APIs Using AI (with LangGraph)....306
Chatting with Your Agent (with Tools)....308
Additional Resources....309
Summary....310
Chapter 15. Using ChatGPT to Call Your API....311
Architecture of Your Application....311
Getting Started with ChatGPT....312
Creating a Custom GPT....313
Launching Your GitHub Codespace....316
Running the SportsWorldCentral (SWC) API in GitHub Codespaces....317
Adding the Servers Section to Your OAS File....318
Creating a GPT Action....319
Testing the APIs in Your GPT....321
Chatting with Your Custom GPT....322
Completing Your Part III Portfolio Project....324
Summary....326
Index....327
About the Author....352
Colophon....352
Are you ready to grow your skills in AI and data science? A great place to start is learning to build and use APIs in real-world data and AI projects. API skills have become essential for AI and data science success, because they are used in a variety of ways in these fields. With this practical book, data scientists and software developers will gain hands-on experience developing and using APIs with the Python programming language and popular frameworks like FastAPI and StreamLit.