# Load job postings data into R

> Fetch job posts into an R data frame with httr2 and dplyr - pagination included.

Page: https://jobdataapi.com/integrations/r/
Category: Programming languages
You need: R 4.1+, install.packages(c("httr2", "dplyr")), API key in the JOBDATA_API_KEY environment variable and a jobdata API key (https://jobdataapi.com/accounts/pricing/).

## Steps

1. Set JOBDATA_API_KEY in your .Renviron.
2. Call fetch_jobs() with any filters as a named list.
3. Pick the fields you need into a tibble and analyze with dplyr or ggplot2.

## Code

```r
library(httr2)
library(dplyr)

auth <- paste("Api-Key", Sys.getenv("JOBDATA_API_KEY"))

fetch_jobs <- function(query) {
  req <- request("https://jobdataapi.com/api/jobs/") |>
    req_headers(Authorization = auth) |>
    req_url_query(!!!query, page_size = 1000)
  jobs <- list()
  repeat {
    data <- req |> req_perform() |> resp_body_json()
    jobs <- c(jobs, data$results)
    if (is.null(data$`next`)) break
    req <- request(data$`next`) |> req_headers(Authorization = auth)
  }
  jobs
}

jobs <- fetch_jobs(list(title = "data analyst", max_age = 30))
df <- bind_rows(lapply(jobs, function(j) tibble(
  id = j$id,
  title = j$title,
  company = j$company$name,
  published = j$published,
  application_url = j$application_url
)))
count(df, company, sort = TRUE)
```

## Notes

- next is a reserved word in R, hence the backticks in data$`next`.
- Run it daily with max_age=1 to pick up new job posts; on access lite, drop max_age - the feed covers the past 90 days and upserting by id keeps it free of duplicates.

## Related docs

- [Jobs API Ingestion Guide: Reliable Historical Backfills and Incremental Syncs](https://jobdataapi.com/c/jobs-api-ingestion-guide-reliable-historical-backfills-and-incremental-syncs/)
- [Optimizing API Requests: A Guide to Efficient jobdata API Usage](https://jobdataapi.com/c/optimizing-api-requests-a-guide-to-efficient-jobdata-api-usage/)

