JavaScript / Node.js SDK

This is the Node.js client for AYLIEN News API.

View on gitHub npm version

Installation

Install the library at the command prompt if you haven't yet:

npm

npm install aylien-news-api

git

npm install AYLIEN/aylien_newsapi_nodejs

Upgrading to the latest Version

To upgrade to the latest version, use the following command. Remember to upgrade any relevant dependency files in which you specify the version of the SDK (e.g. a requirements.txt for python, a go.mod for go, etc.).

npm

npm update aylien-news-api

Getting Started

Once you have installed the library, you can execute the following code sample:

var AylienNewsApi = require("aylien-news-api");

var defaultClient = AylienNewsApi.ApiClient.instance;

var app_id = defaultClient.authentications["app_id"];
app_id.apiKey = process.env["NEWSAPI_APP_ID"];

var app_key = defaultClient.authentications["app_key"];
app_key.apiKey = process.env["NEWSAPI_APP_KEY"];

var api = new AylienNewsApi.DefaultApi();

var opts = {
  title: "startup",
  publishedAtStart: "NOW-7DAYS",
  publishedAtEnd: "NOW"
};

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log("API called successfully. Returned data: ");
    console.log("========================================");
    for (var i = 0; i < data.stories.length; i++) {
      console.log(data.stories[i].title + " / " + data.stories[i].source.name);
    }
  }
};

api.listStories(opts, callback);

Migration from the Legacy News API Node SDK

Client initialization

If you are migrating from old version of the SDK, you should apply the following changes, as the defaultClient initialization has changed.

In the old version:

var apiInstance = new AylienNewsApi.DefaultApi();
var defaultClient = apiInstance.apiClient;

In the new version:

var apiInstance = new AylienNewsApi.DefaultApi()
var defaultClient = AylienNewsApi.ApiClient.instance;

Response field names

The field names in the JSONified response story objects have changed from camelCase to snake_case, to conform with the styling of objects to match what the API outputs.

For example, in the old version, JSONify(stories[0]) will return a story's timestamp in the publishedAt (camelCase) field:

publishedAt: "2020-01-01T10:09:08.000Z",

Whereas in the new version, the field is in snake_case:

published_at: "2020-01-01T10:09:08.000Z",

The method calling the Related Stories endpoint was previously:

listRelatedStories()

Whereas in the current SDK it is:

listRelatedStoriesGet()

Python SDK

This is the Python client for AYLIEN News API which is compatible with Python 2.7 and 3.4+.

View on gitHub PyPI version

Installation

PyPI

Install it directly from PyPI repository:

pip install -U aylien-news-api

(or sudo pip install -U aylien-news-api to install the package for all users)

git

Install it via:

pip install git+https://github.com/AYLIEN/aylien_newsapi_python.git

(you may need to run pip with root permission: sudo pip install git+https://github.com/AYLIEN/aylien_newsapi_python.git)

Upgrading to the latest Version

To upgrade to the latest version, use the following command. Remember to upgrade any relevant dependency files in which you specify the version of the SDK (e.g. a requirements.txt for python, a go.mod for go, etc.).

PyPI

pip install -U aylien-news-api

Getting Started

Once you have installed the library, you can execute the following code sample:

from __future__ import print_function
import time
import aylien_news_api
from aylien_news_api.rest import ApiException
from pprint import pprint

configuration = aylien_news_api.Configuration()
configuration.api_key['X-AYLIEN-NewsAPI-Application-ID'] = 'YOUR_APP_ID'
configuration.api_key['X-AYLIEN-NewsAPI-Application-Key'] = 'YOUR_APP_KEY'

client = aylien_news_api.ApiClient(configuration)
api_instance = aylien_news_api.DefaultApi(client)

try:
    api_response = api_instance.list_stories(
        title='startup',
        published_at_start='NOW-7DAYS',
        published_at_end='NOW'
    )
    pprint(api_response)
except ApiException as e:
    print("Exception when calling DefaultApi->list_stories: %s\n" % e)

Migration from the Legacy News API Python SDK

If you are migrating from the Legacy News API, please note the following change you need to make in your workflow.

Configuration

Configuration and Authentication is different, as configuration is now a separate class

In the old version:

aylien_news_api.configuration.api_key['X-AYLIEN-NewsAPI-Application-ID'] = 'YOUR_APP_ID'
aylien_news_api.configuration.api_key['X-AYLIEN-NewsAPI-Application-Key'] = 'YOUR_APP_KEY'

api_instance = aylien_news_api.DefaultApi()

In the new version:

configuration = aylien_news_api.Configuration()
configuration.api_key['X-AYLIEN-NewsAPI-Application-ID'] = 'YOUR_APP_ID'
configuration.api_key['X-AYLIEN-NewsAPI-Application-Key'] = 'YOUR_APP_KEY'

client = aylien_news_api.ApiClient(configuration)
api_instance = aylien_news_api.DefaultApi(client)

Go SDK

This is the Go client for AYLIEN News API.

View on gitHub

Installation

To install, simply use go get:

go get github.com/AYLIEN/aylien_newsapi_go

Upgrading to the latest Version

To upgrade to the latest version, use the following command. Remember to upgrade any relevant dependency files in which you specify the version of the SDK (e.g. a requirements.txt for python, a go.mod for go, etc.).

Go

go get -u github.com/AYLIEN/aylien_newsapi_go/v5

or

go install github.com/aylien/aylien_newsapi_go/v5@latest

Getting Started

Once you have installed the client, you can execute the following code sample:

package main

// Import the library
import (
    "context"
    "fmt"
    newsapi "github.com/AYLIEN/aylien_newsapi_go"
    "os"

    "github.com/antihax/optional"
)

func main() {
    cfg := newsapi.NewConfiguration()
    cfg.DefaultHeader["X-AYLIEN-NewsAPI-Application-ID"] = os.Getenv("NEWSAPI_APP_ID")

    // Configure API key authorization: app_key
    cfg.DefaultHeader["X-AYLIEN-NewsAPI-Application-Key"] = os.Getenv("NEWSAPI_APP_KEY")

    client := newsapi.NewAPIClient(cfg)
    api := client.DefaultApi

    storiesParams := &newsapi.ListStoriesOpts{
        Title:            optional.NewString("startup"),
        PublishedAtStart: optional.NewString("NOW-7DAYS"),
        PublishedAtEnd:   optional.NewString("NOW"),
    }

    storiesResponse, res, err := api.ListStories(context.Background(), storiesParams)
    if err != nil {
        panic(err)
    }
    _ = res

    for _, story := range storiesResponse.Stories {
        fmt.Println(story.Title, " / ", story.Source.Name)
    }
}

Migration from the Legacy News API Go SDK

If you are migrating from old version of the SDK, you should apply the following changes:

Configuration

Configuration and Authentication has changed a little, since configuration is now a separate class:

In the old version:

import (
  newsapi "github.com/AYLIEN/aylien_newsapi_go"
)

api := newsapi.NewDefaultApi()
api.Configuration.APIKeyPrefix["X-AYLIEN-NewsAPI-Application-ID"] = "YOUR_APP_ID"
api.Configuration.APIKeyPrefix["X-AYLIEN-NewsAPI-Application-Key"] = "YOUR_APP_KEY"

In the new version:

import (
  newsapi "github.com/AYLIEN/aylien_newsapi_go"
)

cfg := newsapi.NewConfiguration()
cfg.DefaultHeader["X-AYLIEN-NewsAPI-Application-ID"] = "YOUR_APP_ID"
cfg.DefaultHeader["X-AYLIEN-NewsAPI-Application-Key"] = "YOUR_APP_KEY"

client := newsapi.NewAPIClient(cfg)
api := client.DefaultApi

Function signatures

The SDK now uses the antihax/optional library for all parameters, and therefore the function signatures have changed:

In the old version:

import (
  newsapi "github.com/AYLIEN/aylien_newsapi_go"
)
...
storiesParams := &newsapi.StoriesParams{
  Title:            "google",
  PublishedAtStart: "NOW-7DAYS",
}
storiesResponse, res, err := api.ListStories(storiesParams)

In the new version:

import (
  newsapi "github.com/AYLIEN/aylien_newsapi_go"
  "context"
  "github.com/antihax/optional"
)
...
storiesParams := &newsapi.ListStoriesOpts{
  Title:            optional.NewString("google"),
  PublishedAtStart: optional.NewString("NOW-7DAYS"),
}
storiesResponse, res, err := api.ListStories(context.Background(), storiesParams)

Ruby SDK

This is the Ruby client for AYLIEN News API.

View on gitHub Gem Version

Installation

RubyGems

Add this to the Gemfile:

gem 'aylien_news_api'

or install it directly:

gem install aylien_news_api

git

Add the following to your Gemfile:

gem 'aylien_news_api', :git => 'https://github.com/AYLIEN/aylien_newsapi_ruby.git'

Upgrading to the latest Version

To upgrade to the latest version, use the following command. Remember to upgrade any relevant dependency files in which you specify the version of the SDK (e.g. a requirements.txt for python, a go.mod for go, etc.).

RubyGems

bundle update aylien_news_api

Getting Started

Once you have installed the library, you can execute the following code sample:

# Load the gem
require 'aylien_news_api'

# Setup authorization
AylienNewsApi.configure do |config|
  config.api_key['X-AYLIEN-NewsAPI-Application-ID'] = ENV['NEWSAPI_APP_ID']
  config.api_key['X-AYLIEN-NewsAPI-Application-Key'] = ENV['NEWSAPI_APP_KEY']
end

api_instance = AylienNewsApi::DefaultApi.new

opts = {
  title: 'startup',
  published_at_start: "NOW-7DAYS",
  published_at_end: "NOW"
}


begin
  result = api_instance.list_stories(opts)
  result.stories.each do |story|
    puts "#{story.title} / #{story.source.name}"
  end
rescue AylienNewsApi::ApiError => e
  puts "Exception when calling DefaultApi->list_stories: #{e}"
  puts e.response_body
end