book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Tools

Search

Introduction

Search for content in QuantConnect.

Request

Search for content in QuantConnect. The /ai/tools/search API accepts requests in the following format:

SearchRequest Model - Request to search content in QuantConnect.
language string Enum
required
example: Py

Programming language of the content to search. Options : ['C#', 'Py']
criteria SearchCriteria Array
required
example: [{"input": "option", "type": "Docs", "count": 1}]

Criteria for the search.
Example
{
  "language": "Py",
  "criteria": "[{"input": "option", "type": "Docs", "count": 1}]"
  ]
}
SearchCriteria Model - Search criteria.
input string
example: option

Input for the search.
type string
example: Docs

Type of the search criteria.
count integer
example: 1

Number of results to return.
Example
{
  "input": "option",
  "type": "Docs",
  "count": 1
}

Responses

The /ai/tools/search API provides a response in the following format:

200 Success

SearchResponse Model - Response to a search request.
state string
example: End

State of the search.
version string
example: 2.0

Version of the response.
retrivals SearchRetrieval Array
List of search results.
messageId integer
example: 0

ID of the message.
Example
{
  "state": "End",
  "version": 2.0,
  "retrivals": [
    {
      "url": "option[Index Options - QuantConnect.com](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/index-options)",
      "score": 0.320344448,
      "content": "string",
      "type": 2
    }
  ],
  "messageId": 0
}
SearchRetrieval Model - Search criteria.
url string
example: option[Index Options - QuantConnect.com](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/index-options)

Input for the search.
score number
example: 0.320344448

Relevance score of the search result.
content string
Content of the search result.
type number
example: 2

Type of the search result.
Example
{
  "url": "option[Index Options - QuantConnect.com](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/index-options)",
  "score": 0.320344448,
  "content": "string",
  "type": 2
}

401 Authentication Error

UnauthorizedError Model - Unauthorized response from the API. Key is missing, invalid, or timestamp is too old for hash.
www_authenticate string
Header

Examples

The following example demonstates searching content in QuantConnect through the cloud API.

from base64 import b64encode
from hashlib import sha256
from time import time
from requests import get, post
BASE_URL = 'https://www.quantconnect.com/api/v2/'

# You need to replace these with your actual credentials.
# You can request your credentials at https://www.quantconnect.com/settings/
# You can find our organization ID at https://www.quantconnect.com/organization/ 
USER_ID = 0
API_TOKEN = '____'
ORGANIZATION_ID = '____'

def get_headers():
    # Get timestamp
    timestamp = f'{int(time())}'
    time_stamped_token = f'{API_TOKEN}:{timestamp}'.encode('utf-8')

    # Get hased API token
    hashed_token = sha256(time_stamped_token).hexdigest()
    authentication = f'{USER_ID}:{hashed_token}'.encode('utf-8')
    authentication = b64encode(authentication).decode('ascii')

    # Create headers dictionary.
    return {
        'Authorization': f'Basic {authentication}',
        'Timestamp': timestamp
    }

# Authenticate to verify credentials
response = post(f'{BASE_URL}/authenticate', headers = get_headers())
print(response.json())

# --------------------


### Search Content
# Send a POST request to the /ai/tools/search endpoint to search content
response = post(f'{BASE_URL}/ai/tools/search', headers=get_headers(), json={
    "language": "Python",  # Programming language to filter search results
    "criteria": [  # Search criteria
        {
            "input": "option",  # Search term (e.g., "option")
            "type": "Docs",  # Type of content to search (e.g., documentation)
            "count": 1  # Number of results to return
        }
    ]
})
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the search results
if result['success']:
    print("Search Results:")
    print(result)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: