A command-line interface for downloading data from NewsAPI.ai. It currently only supports the article/getArticles endpoint.
You can spec your query by hard, but the easiest approach is in conjunction with NewsAPI.ai's sandbox: simply save the REST output to a file and run the cli against it to download your data.
git clone https://github.com/specialprocedures/napi.git
cd napi
pip install -e .napi pull query.json output.json --api_key YOUR_API_KEYYou can provide your API key to napi in several ways. The easiest is to use the query as is from the sandbox, which should include your key in the apiKey field.
If you'd like to avoid keeping your key in source control, you have several options.
The tool looks for API keys in the following order:
- In the source json
--api_keycommand line argumentNEWSAPI_API_KEYenvironment variableNEWSAPI_API_KEYin a.envfile
Set your API key as an environment variable:
export NEWSAPI_API_KEY=your_api_key_here
napi pull query.json output.jsonCreate a .env file in your working directory:
NEWSAPI_API_KEY=your_api_key_here
Then run:
napi pull query.json output.jsonThe query file should be in JSON format as generated by the NewsAPI sandbox. Example:
{
"query": {
"$query": {
"$and": [
{
"conceptUri": "http://en.wikipedia.org/wiki/Gaza"
},
{
"dateStart": "2023-08-06",
"dateEnd": "2025-10-07",
"lang": "eng"
}
]
}
},
"resultType": "articles",
"articlesSortBy": "date"
}git clone https://github.com/yourusername/napi.git
cd napi
pip install -e .python -m pytest tests/I've used some variation on this very simple code in numerous projects over the past few years. Given how frequently I need to download from NewsAPI, I figured I'd wrap it up in a cli and publish it for others to use.
The initial core functions, i.e., those retrieving the request response, were written by me. I then used Copilot (and Claude Sonnet 4) to:
- Wrap the functions for cli use with argparse
- Bootstrap tests, docs and project scaffolding
- Start getting things ready for publication to PyPI (forthcoming)
MIT License - see LICENSE file for details.
