Este contenido no está disponible en el idioma seleccionado.

Chapter 3. Making API calls


There are multiple ways to make API calls to the Red Hat Insights APIs. Some of the available methods include:

  • Using the curl command from your command line or script
  • Accessing the Insights API documentation within the Hybrid Cloud Console
  • Referring to the legacy documentation within the Hybrid Cloud Console
  • Making calls through a Python interpreter or other programming languages in your client code
Important

You should have a service account set up and configured for authentication in the application or system that will be performing the API calls. For more information about authentication, see Chapter 2, Configuring Authentication with Insights APIs.

The Red Hat Developers API Catalog helps you to create valid API calls in a variety of languages and formats, including curl commands. For example, you can send an API call in a curl command or embed a python-formatted API call in python code. Using the GET/api/insights/v1/account API call in the command or in code returns a list of all known accounts by Organization ID (org_id).

3.1. Using the curl command

You can use the curl command to communicate with a server over HTTP without having to use a browser. curl is useful for making quick requests from the command line, such as requesting status, making updates to parameters, or requesting the values for specific parameters.

Note

This example procedure uses the Advisor API endpoint.

Prerequisites

  • You have an access token that you obtained from your service account or from an offline access token for authentication.
  • The service account has the necessary permissions to interact with the requested endpoint.

Procedure

  1. Open the API catalog in a web browser. The API catalog lists available APIs for Insights and other services.
  2. Select Advisor from the catalog. The Insights Advisor API page opens. The page lists the base URL of the Advisor API and any requirements for authentication.
  3. Under the Operations list, click the drop-down arrow next to GET /api/insights/v1/account.

    The description for the operation includes parameters you can use to refine your API call, and also expected responses from the server. In addition, the panel on the right side of the page has sample code to perform the API call in many languages.

  4. In the panel, click the drop-down and select CURL from the list of options. The panel displays sample code for the GET operation, formatted as a curl command.
  5. Copy the sample code and paste it into your command line or into an existing code file where you want to invoke the GET command.

    # curl --request GET \
    --url https://console.redhat.com/api/insights/v1/account/ \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json'
    Copy to Clipboard Toggle word wrap
  6. Paste your access token in place of <token>.
  7. Press Enter to invoke the command.

If the command line returns a response of 200, your API call was successful. In addition to a successful response and an optional description of the operation, the server returns the data you requested.

If the command line returns a response other than 200, refer to Response Codes to determine how the API call failed and how to remedy the reason for the failure.

Additional resources

  • The Red Hat Insights API cheat sheet contains information about how to authenticate your API access, and includes examples of common queries for Insights services. The cheat sheet also contains examples for python and for Ansible. To download the cheat sheet, navigate to Red Hat Insights API cheat sheet.
  • Red Hat Developers API catalog: https://developers.redhat.com/api-catalog

3.2. Using the Insights API Catalog and Documentation

The Insights API Catalog and Documentation page in the Red Hat Hybrid Cloud Console describes each operation you can perform with the Red Hat Insights API. The documentation describes how to create API requests in multiple languages and formats. You can copy the requests and then use them within your own code to make the API calls.

The Red Hat Developers API Catalog shows you how to create valid API calls in a variety of languages and formats, including python code. For example, the GET/api/insights/v1/account API call returns a list of all known accounts by Organization ID (org_id).

Note

This example procedure uses the Advisor API endpoint.

Prerequisites

  • You have the programming language you want to use (for example, python) installed on your system.
  • You have an access token that you obtained from your service account or from an offline access token.
  • You have a service account configured with necessary permissions.

Procedure

  1. Open the API catalog in a web browser. The API catalog lists available APIs for Insights and other services.
  2. Select Advisor from the catalog. The Insights Advisor API page opens. The page lists the base URL of the Advisor API and any requirements for authentication.
  3. Under the Operations list, click the drop-down arrow next to GET /api/insights/v1/account. The description for the operation includes parameters you can use to refine your API call, as well as expected responses from the server. In addition, the panel on the right side of the page generates an example API call for the GET operation in multiple programming languages.
  4. In the panel, click the drop-down and select the programming language that you prefer (for example, Python) from the list of options. The panel displays sample code for the GET operation, formatted in Python syntax.
  5. Copy the sample code and paste it into a Python code file where you want to invoke the GET command. For example:

    import requests
    
    url = "https://console.redhat.com/api/insights/v1/account/"
    
    headers = {
       "Authorization": "Bearer <token>",
       "Content-Type": "application/json"
    }
    
    response = requests.get(url, headers=headers)
    
    print(response.json())
    Copy to Clipboard Toggle word wrap
  6. Paste your authorization key for authentication in place of <token>.
  7. Press Enter to run your code.

If the server returns a response of 200, your API call was successful. In addition to a successful response and an optional description of the operation, the server returns the data you requested.

If the code returns a response other than 200, refer to Response Codes to determine how the API call failed and how to remedy the reason for the failure.

3.2.1. Viewing data formats and schemas

The data formats and schemas for Red Hat Insights API calls display the formats and types for data returned with server responses. You can use this information in your code as well as for troubleshooting unexpected results.

Note

This example uses the Advisor service.

Procedure

  1. Open the API catalog in a web browser. The API catalog lists available APIs for Insights and other services.
  2. Select Advisor from the catalog. The Insights Advisor API page opens. The page lists the base URL of the Advisor API and any requirements for authentication.
  3. Under the Operations list, click the drop-down arrow next to GET /api/insights/v1/account. The Operations section contains the query parameters you need to specify in an API call and the expected format of each parameter (for example, integer).
  4. To see the format for the server response, click the drop-down arrow next to the Responses section. The section shows an example of data that the API returns with a successful response.
  5. To see the formats for the returned data, click the Schema link next to the server response (for example, PaginatedOrgIdList). The page shows the schema for the data in the response, as well as the formats and types of all of the components that make up the data (for example, org_id is a string, and count is an integer).

3.3. Using the Legacy API documentation

The Red Hat Hybrid Cloud Console contains auto-generated legacy Swagger documentation for Insights API operations. You can customize the provided example code for API calls, execute the requests from within the documentation, and receive the server response code.

Prerequisites

  • You are logged in to the Red Hat Hybrid Cloud Console.
  • You have the necessary permissions to perform the queries.

Procedure

  1. Navigate to the API documentation page in the Red Hat Hybrid Cloud Console.
  2. Select the application that you want to use from the list (for example, Advisor). The page for the application displays, with a list of all the operations you can request for that application.
  3. Select the operation that you want to perform (for example, Advisor > account > GET /api/insights/v1/account).
  4. Click the drop-down arrow for the operation. The Swagger documentation page describes the operation and lists the parameters you can use to refine your request. The page also shows the JSON format for a successful server response.
  5. Click Try It Out. The page displays the format of a successful server response. If the operation you selected requires a value for a parameter (for example, org_id), enter the value in the blank.
  6. Click Execute. The page displays a curl request to the server, the request URL, and the server response.

    1. To copy the curl request to the clipboard, click the clipboard icon on the right side of the curl request.
    2. To copy the server response to the clipboard, click the clipboard icon in the top right corner.
    3. To download the server response as a JSON file, click Download.
  7. Optional: To clear the server response and retry the command, click Clear.

You can use the generated requests in your own code to perform API calls to the Insights API server. For POST requests, you can paste formatted data into the Request Body section of the request and select the format for the data (for example, application/json).

For detailed information about the parameters, schemas, and descriptions for each operation, click the View JSON or RAW links on the API Documentation page. Both links display the operations, required formats for parameters, detailed descriptions for each operation, and the required information within each schema. The RAW link also displays information about the header information for both server requests and responses.

For more examples of common requests using Insights APIs, download the Red Hat Insights API cheat sheet.

Note

You must have a login at developers.redhat.com to download the cheat sheet.

Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat