Using API Keys with the CFBD API
As you are no doubt aware by now, the enforcement of key authentication has gone into affect for the CFBD API. We won't rehash all of the particulars about the decision to implement key-based auth, but I still wanted to do a quick primer on their usage since there still seems to be some lingering questions.
First thing's first, if you haven't yet registered for a key of your own, be sure to go ahead and do that. It's quick, it's painless, and it's completely free. As far as usage, you can also refer to the API docs, but there are some caveats due to some limitations of the document generation tool being used.
Once you have a key, using it is actually quite simple. Let's say I have a personal API key. We'll call it mysupersecretpersonalkey. In order to use it to make requests, you merely need to add an Authorization header to your request with a value of "Bearer mysupersecretpersonalkey" but without the quotes. Keep reading to see how to implement this using various difference languages and libraries.
curl
curl -X GET "https://api.collegefootballdata.com/games?year=2020&seasonType=regular" -H "accept: application/json" -H "Authorization: Bearer mysupersecretpersonalkey"
Python (using cfbd)
import cfbd
configuration = cfbd.Configuration()
configuration.api_key['Authorization'] = 'YOUR_API_KEY'
configuration.api_key_prefix['Authorization'] = 'Bearer'
api_instance = cfbd.GamesApi(cfbd.ApiClient(configuration))
games = api_instance.get_games(year=2020)
JavaScript/NodeJS (using cfb.js)
const cfbd = require('cfb.js');
const defaultClient = cfb.ApiClient.instance;
const ApiKeyAuth = defaultClient.authentications['ApiKeyAuth'];
ApiKeyAuth.apiKey = "mysupersecretpersonalkey";
ApiKeyAuth.apiKeyPrefix["Authorization"] = "Bearer";
var api = new cfb.GamesApi();
let year = 2020;
const games = await api.getGames(year);
C#/.NET (using CFBSharp)
using System;
using CFBSharp.Api;
using CFBSharp.Client;
using CFBSharp.Model;
namespace Example
{
public class Example
{
public void main()
{
Configuration.Default.ApiKey.Add("Authorization", "mysupersecretpersonalkey");
Configuration.Default.ApiKeyPrefix.Add("Authorization", "Bearer");
var api = new GamesApi();
int year = 2020;
var results = apiInstance.GetGames(year);
}
}
}
R
Right now, there is no official CFBD wrapper library for R since the independently maintained cfbscrapR package exists and already provides a robust wrapper around the CFBD API. Refer to the official cfbscrapR docs for how to use API keys with R. As of this writing, API key functionality hadn't been implement but was actively being worked on.
Other Languages
This article covers all officially supported wrapper libraries plus standard curl and the popular cfbscrapR package for R. It should be possible to figure this out for other languages by following the example for curl and using a standard HTTP library. I may come back and update this article with examples in more languages upon request. Anyway, hopefully this all helps. Good luck!