New Relic API Python Client

1.0.6 · maintenance · verified Thu Apr 16

The `newrelic-api` library provides a Python interface to interact with the New Relic API v2. It allows users to fetch application data, monitor metrics, and manage various New Relic entities programmatically. Currently at version 1.0.6, it is a community-maintained client that sees infrequent releases, primarily for maintenance and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the New Relic API client using an API key from an environment variable and fetches a list of applications. It demonstrates basic API interaction and error handling.

import os
from newrelic_api import NewRelicApi

# Get your New Relic REST API Key (v2) from environment variable
# Ensure it's not a License Key or Insights Key
NEW_RELIC_API_KEY = os.environ.get('NEW_RELIC_API_KEY', '')

if not NEW_RELIC_API_KEY:
    print("Error: NEW_RELIC_API_KEY environment variable not set.")
    exit(1)

api = NewRelicApi(api_key=NEW_RELIC_API_KEY)

try:
    # Fetch all applications associated with the API key
    applications = api.get_applications()
    print(f"Found {len(applications)} applications:")
    for app in applications:
        print(f"  ID: {app.get('id')}, Name: {app.get('name')}")

    # Example: Get a specific application by ID (replace with a real ID)
    if applications:
        first_app_id = applications[0]['id']
        specific_app = api.get_application(first_app_id)
        print(f"\nDetails for first application ({first_app_id}): {specific_app.get('name')}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →