shamithewebdeveloper/laravel-nytimes-api

A Laravel package for working with the New York Times API.

Maintainers

Package info

github.com/ShamiTheWebdeveloper/laravel-nytimes-api

pkg:composer/shamithewebdeveloper/laravel-nytimes-api

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.2 2025-08-29 16:44 UTC

This package is auto-updated.

Last update: 2026-04-04 13:22:08 UTC


README

A lightweight Laravel package to easily integrate the New York Times API into your Laravel applications. Fetch the latest news, articles, and top stories directly from NYTimes with simple and elegant syntax.

🚀 Features

  • Simple integration with the New York Times API
  • Supports Top Stories, Most Popular, and Article Search endpoints
  • Built-in Facade (NYTimes) and helper methods
  • Powered by GuzzleHTTP for secure API requests
  • Compatible with Laravel 8, 9, 10, 11, and 12

📦 Installation

  composer require shamithewebdeveloper/laravel-nytimes-api

🔧 Configuration

  1. Publish the config file:
  php artisan vendor:publish --tag=nytimes-config
  1. Add your NYTimes API Key(Public Key) in .env:

NYTIMES_API_KEY=your_api_key_here

  1. Add this to the service provider:
ShamiTheWebdeveloper\NYTimes\NYTimesServiceProvider::class
  1. If you want to use the NYTimes directly in view files (Optional):

Add this to app/config.php (For Laravel 8,9, and 10)

'aliases' => [
        'NYTimes' => \ShamiTheWebdeveloper\NYTimes\Facades\NYTimesFacade::class,
    ]

For Laravel 11 or greater, add this to app/Providers/AppServiceProvider.php in register() function

use ShamiTheWebdeveloper\NYTimes\Facades\NYTimesFacade;
public function register(): void
    {
        $loader = AliasLoader::getInstance();
        $loader->alias('NYTimes', NYTimesFacade::class);
    }

📝 Usage

Retrive Data From API

use ShamiTheWebdeveloper\NYTimes\NYTimes;

NYTimes::searchArticle('Sports', now()->lastWeekDay, now())->get();
// use get() to get response in array

NYTimes::searchArticle('Sports', now()->lastWeekDay, now())->json();
// use json() to get response in json

Get a full response from GuzzleHttp

$articlesFullResponse=NYTimes::searchArticle('Sports', '2024-04-01', now())->fullResponse();
dd($articlesFullResponse);

Get API Request URL

$articleURL=NYTimes::searchArticle('Sports', '2024-04-01', now())->requestURL();
echo $articleURL;

For the views

@dd(NYTimes::newsSectionList()->get())

Examples

For Article Search:

/*
Parameters
$query (string required), $start_date (string), $end_date (string), $filter_query (string), $sort (string), $page=1 (int)
*/

$articles=NYTimes::searchArticle('Sports', now()->lastWeekDay, now())->get();

foreach ($articles['response']['docs'] as $article)
{
  echo $article['abstract'].'<br>';
  echo $article['web_url'].'<br>';
}

For Archive:

/*
Parameters
$month (int required), $year (int required)
*/

$archives= NYTimes::archive(5,2025)->get();

foreach ($archives['response']['docs'] as $archive) {
    echo $archive['abstract'].'<br>';
    echo $archive['section_name'].'<br>';
    echo $archive['word_count'].'<br>';
}

For Most Popular:

/*
Parameters
$type (string required), $period (int), $shareType (string)
*/

$populars= NYTimes::mostPopular('emailed',7)->get();

foreach ($populars['results'] as $popular) {
    echo $popular['section'].'<br>';
    echo $popular['title'].'<br>';
    echo $popular['abstract'].'<br>';
}

For Books (Overview)

/*
Parameters
$publishedDate (string)
*/

$books=NYTimes::bookOverview('2024-05-04')->get();

echo $books['results']['published_date'].'<br>';
echo '<h3>Books:</h3>';

foreach ($books['results']['lists'] as $booklist)
{
  echo $booklist['list_name'].'<br>';

  foreach ($booklist['books'] as $book)
  {
    echo $book['title'].'<br>';
    echo $book['author'].'<br>';
  }
}

For Books (List)

/*
Parameters
$list (string required) ,$date (string)
*/

$books=NYTimes::bookList('series-books')->get();

echo '<h2>'.$books['results']['list_name'].'</h2>';

foreach ($books['results']['books'] as $book)
{
    echo $book['title'].'<br>';
    echo $book['author'].'<br>';
}

For Times Newswire

/*
Parameters
$source (string required) ,$section (string) ,$limit (int), $offset (int)
*/

$news=NYTimes::timesNewswire('nyt','all',30,20)->get();

foreach ($news['results'] as $new)
{
    echo $new['title'].'<br>';
    echo $new['abstract'].'<br>';
    echo $new['url'].'<br>';
}

For Times Newswire (Get News By URL)

/*
Parameters
$url (string),
*/

$url='https://www.nytimes.com/2025/08/25/arts/dance/kennedy-center-stephen-nakagawa.html';

$news=NYTimes::timesNewswireUrl($url)->get();

foreach ($news['results'] as $new)
{
    echo $new['title'].'<br>';
    echo $new['abstract'].'<br>';
}

Get News Sections

$sections=NYTimes::newsSectionList()->get();

foreach ($sections['results'] as $section)
{
    echo $section['section'].'<br>';
    echo $section['display_name'].'<br>';
}

For Top Stories

/*
Parameters
$sections (string),
*/

$stories=NYTimes::topStories('food')->get();

foreach ($stories['results'] as $story)
{
    echo $story['title'].'<br>';
    echo $story['abstract'].'<br>';
    echo $story['url'].'<br>';
}

For RSS

/*
Parameters
$section (string),
*/

$feeds=NYTimes::rssFeed('Automobiles')->get();

echo $feeds['channel']['title'].'<br>';

foreach ($feeds['channel']['item'] as $feed)
{
    echo $feed['title'].'<br>';
    echo $feed['link'].'<br>';
    echo $feed['description'].'<br>';
}
// returns data in XML (toXML() Only valid for RSS Feed)
$feeds=NYTimes::rssFeed('Automobiles')->toXML();
header('Content-type: application/xml');
echo $feeds;

📌 Requirements

PHP >= 7.4

Laravel 8, 9, 10, 11 or 12

NYTimes API Key (Free – Get one here (https://developer.nytimes.com)

🤝 Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request to improve this package.

📜 License

This package is open-sourced software licensed under the MIT license.