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

Programming language of the content to search. Options : ['C#', 'Py']
criteria SearchCriteria Array
required

Criteria for the search.
Example
{
  "language": "C#",
  "criteria": [
    {
      "input": "string",
      "type": "Stubs",
      "count": 0
    }
  ]
}
SearchCriteria Model - Search criteria.
input string
required

Input for the search.
type string Enum
required

Type of the search criteria. Options : ['Stubs', 'Forum', 'Docs', 'Examples']
count integer
required

Number of results to return.
Example
{
  "input": "string",
  "type": "Stubs",
  "count": 0
}

Responses

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

200 Success

SearchResponse Model - Response to a search request.
state string Enum
State of the search. Options : ['End', 'Error']
version number
Version of the response.
retrivals SearchRetrieval Array
List of search results.
messageId integer
Id of the message.
Example
{
  "state": "End",
  "version": 0,
  "retrivals": [
    {
      "url": "string",
      "score": 0,
      "content": "string",
      "type": 0
    }
  ],
  "messageId": 0
}
SearchRetrieval Model - Search criteria.
url string
Input for the search.
score number
Relevance score of the search result.
content string
Content of the search result.
type number
Type of the search result. 0=Stubs, 1=Forum, 2=Docs, 3=Examples.
Example
{
  "url": "string",
  "score": 0,
  "content": "string",
  "type": 0
}

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: