Chapter 5. Enabling and using the Red Hat Quay API
The Red Hat Quay API provides endpoints to automate registry tasks such as repository, user, and image management. You can integrate these endpoints into your existing workflows.
5.1. Using the Red Hat Quay API Copy linkLink copied to clipboard!
To invoke Red Hat Quay API endpoints from the CLI, you can pass an OAuth 2 access token in a curl request. You can use GET, PUT, POST, or DELETE methods against the documented endpoints.
After you have created an application and generated an OAuth 2 access token with the desired settings, you can pass in the access token to GET, PUT, POST, or DELETE settings by using the API from the CLI. Generally, a Red Hat Quay API command looks similar to the following example:
$ curl -X GET -H "Authorization: Bearer <your_access_token>" \
https://<quay-server.example.com>/api/v1/<example>/<endpoint>/
+ where:
<your_access_token>- Specifies the OAuth 2 access token that was generated through the Red Hat Quay UI.
https://<quay-server.example.com>/api/v1/<example>/<endpoint>/- Specifies your Red Hat Quay deployment and the desired API endpoint.
All Red Hat Quay APIs are documented in the Application Programming Interface (API) chapter. Understanding how they are documented is crucial to successful invocation. Take, for example, the following entry for the createAppToken API endpoint:
*createAppToken*
Create a new app specific token for user.
*POST /api/v1/user/apptoken*
**Authorizations: **oauth2_implicit (**user:admin**)
Request body schema (application/json)
*Path parameters*
Name: **title**
Description: Friendly name to help identify the token.
Schema: string
*Responses*
|HTTP Code|Description |Schema
|201 |Successful creation |
|400 |Bad Request |<<_apierror,ApiError>>
|401 |Session required |<<_apierror,ApiError>>
|403 |Unauthorized access |<<_apierror,ApiError>>
|404 |Not found |<<_apierror,ApiError>>
|===
+ where:
createAppToken- Specifies the name of the API endpoint.
Create a new app specific token for user.- Specifies a brief description of the API endpoint.
POST /api/v1/user/apptoken- Specifies the API endpoint used for invocation.
Authorizations- Specifies the authorizations required to use the API endpoint.
Path parameters-
Specifies the available paths to be used with the API endpoint. In this example,
titleis the only path to be used with thePOST /api/v1/user/apptokenendpoint. Responses- Specifies the API responses for this endpoint.
5.2. Using a Red Hat Quay API endpoint Copy linkLink copied to clipboard!
To use an API endpoint, you pass in your access token and then include the appropriate fields depending on your needs.
Prerequisites
- You have access to the Red Hat Quay API, which entails having already created an OAuth 2 access token.
Procedure
Create a user application by entering the
POST /api/v1/user/apptokenAPI call:$ curl -X POST \ -H "Authorization: Bearer <access_token>" \ -H "Content-Type: application/json" \ -d '{ "title": "MyAppToken" }' \ "http://quay-server.example.com/api/v1/user/apptoken"where:
<access_token>- Specifies the OAuth access token.
"MyAppToken"- Specifies the name of your application token.
http://quay-server.example.com/api/v1/user/apptokenSpecifies your Red Hat Quay deployment appended with the
/api/v1/user/apptokenendpoint.Example output
{"token": {"uuid": "6b5aa827-cee5-4fbe-a434-4b7b8a245ca7", "title": "MyAppToken", "last_accessed": null, "created": "Wed, 08 Jan 2025 19:32:48 -0000", "expiration": null, "token_code": "string"}}
Verification
On the Red Hat Quay UI, click your username in the navigation pane
Account Settings. The name of your application appears under the Docker CLI and other Application Tokens heading. For example:
5.3. Enabling browser-based API calls in Red Hat Quay Copy linkLink copied to clipboard!
To use Red Hat Quay API access from a browser extension or Swagger UI, you can disable BROWSER_API_CALLS_XHR_ONLY in your config.yaml file.
By default, Red Hat Quay accepts curl requests from the command line. However, if you want to enable API access from a browser extension such as Postman, or a browser interface such as Swagger, you must disable BROWSER_API_CALLS_XHR_ONLY in your config.yaml file.
Procedure
In your Red Hat Quay
config.yamlfile, setBROWSER_API_CALLS_XHR_ONLYtofalse. For example:# ... BROWSER_API_CALLS_XHR_ONLY: false # ...- Restart your Red Hat Quay deployment.
5.4. Accessing Red Hat Quay Swagger UI Copy linkLink copied to clipboard!
To explore and test Red Hat Quay API endpoints interactively, you can run the Swagger UI container against your registry API discovery endpoint.
Red Hat Quay administrators and users can interact with the API by using the Swagger UI, an interactive web interface that compiles executable commands. The Swagger UI can be launched as a container that points to your Red Hat Quay instance’s API discovery endpoint (/api/v1/discovery). After deploying the container, you can access the Swagger UI, which loads the OpenAPI specification for Red Hat Quay from the specified URL. Red Hat Quay administrators and users can explore the available endpoints and their structure.
Prerequisites
-
You have set
BROWSER_API_CALLS_XHR_ONLY: falsein yourconfig.yamlfile.
Procedure
Enter the following command to deploy the Swagger UI container, pointing the URL to your Red Hat Quay API discovery endpoint. For example:
$ podman run -p 8080:8080 -e SWAGGER_JSON_URL=<quay-server.example.com> docker.swagger.io/swaggerapi/swagger-uiExample output
--- /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh 20-envsubst-on-templates.sh: Running envsubst on /etc/nginx/templates/default.conf.template to /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/40-swagger-ui.sh /docker-entrypoint.sh: Configuration complete; ready for start up ----
Navigate to the
localhostURL. In this example, the URL is http://localhost:8080/. Use the Swagger UI to test various API endpoints. For example, to create a new token for a user, you can click the POST /api/v1/user/apptoken endpoint
Try it out Execute to generate an example curlcommand.NoteCurrently, server responses cannot be generated. This is because the Swagger UI is not configured to accept bearer tokens. As a result, the following error is returned for each command:
{"error": "CSRF token was invalid or missing."}. As a workaround, you can copy this command into your terminal and manually add your bearer token, for example,-H 'Authorization: Bearer <bearer_token>'.
5.5. Automating Red Hat Quay processes by using the API Copy linkLink copied to clipboard!
To automate repetitive Red Hat Quay tasks such as repository management or image pruning, you can call the API from scripts and schedule them with cron.
With the API, Red Hat Quay administrators and users with access to the API can automate repetitive tasks such as repository management or image pruning.
The following example shows you how you might use a Python script and a cron job to automate the deletion of OAuth 2 applications except the administrator’s token. This might be useful if you want to ensure an application associated with an OAuth 2 access token is cycled after a certain period of time.
Prerequisites
- You have access to the Red Hat Quay API, which entails having already created an OAuth 2 access token.
-
You have installed the Python
requestslibrary. - You have enabled cron jobs on your machine.
- You have created several organization applications, including one that you do not want to delete.
Procedure
Create a Python script that executes an API command. The following example deletes organization applications by using the
DELETE /api/v1/organization/{orgname}/applications/{client_id}API endpoint.Create the following
example.pyfile:import requests # Hard-coded values API_BASE_URL = "http://<quay-server.example.com>/api/v1" ACCESS_TOKEN = "<access_token>" ORG_NAME = "<organization_name>" def get_all_organization_applications(): url = f"{API_BASE_URL}/organization/{ORG_NAME}/applications" headers = { "Authorization": f"Bearer {ACCESS_TOKEN}" } response = requests.get(url, headers=headers) if response.status_code == 200: try: applications = response.json() # Print the raw response for debugging print("Raw response:", applications) # Adjust parsing logic based on the response structure if isinstance(applications, dict) and 'applications' in applications: applications = applications['applications'] if isinstance(applications, list): print("Organization applications retrieved successfully:") for app in applications: # Updated key from 'title' to 'name' print(f"Name: {app['name']}, Client ID: {app['client_id']}") return applications else: print("Unexpected response format.") return [] except requests.exceptions.JSONDecodeError: print("Error decoding JSON response:", response.text) return [] else: print(f"Failed to retrieve applications. Status code: {response.status_code}, Response: {response.text}") return [] def delete_organization_application(client_id): url = f"{API_BASE_URL}/organization/{ORG_NAME}/applications/{client_id}" headers = { "Authorization": f"Bearer {ACCESS_TOKEN}" } response = requests.delete(url, headers=headers) if response.status_code == 204: print(f"Application {client_id} deleted successfully.") else: print(f"Failed to delete application {client_id}. Status code: {response.status_code}, Response: {response.text}") def main(): applications = get_all_organization_applications() for app in applications: if app['name'] != "<admin_token_app>": # Skip the "admin-token-app" delete_organization_application(app['client_id']) else: print(f"Skipping deletion of application: {app['name']}") # Execute the main function main()where:
import requests-
Specifies that the
requestslibrary is included in your Python code. API_BASE_URL-
Specifies the URL of your registry appended with
/api/v1. ACCESS_TOKEN- Specifies your OAuth 2 access token.
ORG_NAME- Specifies the organization that holds the application.
"<admin_token_app>"- Specifies the name of the application token to remain.
-
Save the script as
prune_applications.py. Create a cron job that automatically runs the script:
Open the crontab editor by running the following command:
$ crontab -eIn the editor, add the cron job for running the script. The following example runs the script once per month:
0 0 1 * * sudo python /path/to/prune_images.py >> /var/log/prune_images.log 2>&1
5.6. Programmatic OAuth token provisioning Copy linkLink copied to clipboard!
To create and manage organization application tokens without the UI, you can use programmatic OAuth token provisioning through the REST API. You can also enable a Tech Preview bootstrap token for zero-touch automation.
Organization OAuth applications previously required administrators to create API tokens in the Red Hat Quay UI. With programmatic token provisioning, automation tools can manage the token life cycle.
When FEATURE_PROGRAMMATIC_BOOTSTRAP is enabled, Red Hat Quay also creates a high-privilege bootstrap OAuth token on startup and writes it to a local file or Kubernetes Secret. Use the bootstrap token to create organizations, applications, and narrower-scoped tokens without interactive UI access.
Programmatic bootstrap token provisioning is a Tech Preview feature in Red Hat Quay 3.18. Tech Preview features are not supported with Red Hat production service-level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using Tech Preview features in production environments. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.