Deploying Queries

After building queries in Infactory, the next step is to deploy them as API endpoints that applications can use. This guide explains how to deploy queries, manage deployed APIs, and integrate them into your applications.

Understanding the Deploy Page

The Deploy page in Infactory provides tools for managing your deployed queries:

  • Deployed Queries List: All queries that have been deployed as API endpoints
  • Live APIs Section: Interactive Swagger documentation for testing your APIs
  • API Key Management: Tools for creating and managing API keys

Deploying a Query

Once you’ve created and tested a query in the Build page, deploying it is simple:

1

Select a query to deploy

From the Build tab, select the query you want to deploy.

2

Click Deploy

Click the Deploy button at the top of the query editor.

3

Confirm deployment

Review the deployment summary and click Confirm to deploy the query.

4

View in Deploy tab

Navigate to the Deploy tab to see your newly deployed query in the list.

Testing Deployed Queries

You can test your deployed queries directly from the Deploy page:

  1. Click on the Live APIs section
  2. Find your deployed query in the Swagger documentation
  3. Click to expand the query details
  4. Enter values for any parameters (slots)
  5. Click Execute to run the query and see the results

API Architecture

Infactory provides two ways to interact with your deployed queries:

1. Unified API Endpoint

The unified endpoint allows you to send natural language questions and have Infactory automatically route them to the appropriate query:

POST https://api.infactory.ai/v1/query

Request Format

{
  "query": "What is the average height for each position?",
  "project_id": "your_project_id"
}

Response Format

{
  "data": [
    { "position": "Quarterback", "average_height": 75.2 },
    { "position": "Running Back", "average_height": 70.8 },
    { "position": "Wide Receiver", "average_height": 72.5 }
    // ... more results
  ],
  "query_used": "average_by_category",
  "parameters": {
    "category": "position",
    "metric": "height"
  }
}

2. Direct Query Endpoints

You can also call specific query endpoints directly with their parameters:

POST https://api.infactory.ai/v1/queries/{query_name}

Request Format

{
  "category": "position",
  "metric": "height"
}

Response Format

{
  "data": [
    { "position": "Quarterback", "average_height": 75.2 },
    { "position": "Running Back", "average_height": 70.8 },
    { "position": "Wide Receiver", "average_height": 72.5 }
    // ... more results
  ]
}

API Key Management

To secure your API endpoints, Infactory uses API keys:

Creating an API Key

  1. Navigate to the API Keys section in the Deploy tab
  2. Click Create New Key
  3. Enter a name for the key (e.g., “Development”, “Production”)
  4. Select which projects this key should have access to
  5. Click Create
  6. Save the displayed API key securely - it will only be shown once

Using API Keys

Include your API key in all requests to the Infactory API:

const response = await fetch('https://api.infactory.ai/v1/query', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY'
  },
  body: JSON.stringify({
    query: "What is the average height for each position?",
    project_id: "your_project_id"
  })
});

Code Examples

Here are examples of how to integrate with Infactory APIs in different languages:

JavaScript/TypeScript

// Using the unified API endpoint
async function askQuestion(question) {
  const response = await fetch('https://api.infactory.ai/v1/query', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer YOUR_API_KEY'
    },
    body: JSON.stringify({
      query: question,
      project_id: "your_project_id"
    })
  });
  
  return await response.json();
}

// Using a direct query endpoint
async function getAverageByCategory(category, metric) {
  const response = await fetch('https://api.infactory.ai/v1/queries/average_by_category', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer YOUR_API_KEY'
    },
    body: JSON.stringify({
      category: category,
      metric: metric
    })
  });
  
  return await response.json();
}

Python

import requests

# Using the unified API endpoint
def ask_question(question):
    url = "https://api.infactory.ai/v1/query"
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer YOUR_API_KEY"
    }
    payload = {
        "query": question,
        "project_id": "your_project_id"
    }
    
    response = requests.post(url, headers=headers, json=payload)
    return response.json()

# Using a direct query endpoint
def get_average_by_category(category, metric):
    url = "https://api.infactory.ai/v1/queries/average_by_category"
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer YOUR_API_KEY"
    }
    payload = {
        "category": category,
        "metric": metric
    }
    
    response = requests.post(url, headers=headers, json=payload)
    return response.json()

Response Handling

When working with Infactory API responses, consider these best practices:

Error Handling

Always check for and handle errors in your API calls:

try {
  const response = await fetch('https://api.infactory.ai/v1/query', {
    // ... request details
  });
  
  if (!response.ok) {
    const errorData = await response.json();
    console.error("API Error:", errorData);
    // Handle error appropriately
    return;
  }
  
  const data = await response.json();
  // Process successful response
} catch (error) {
  console.error("Network Error:", error);
  // Handle network errors
}

Working with Response Data

The structured data in the response can be used directly in your application:

// Displaying results in a table
function displayResults(data) {
  const table = document.createElement('table');
  const headerRow = document.createElement('tr');
  
  // Create headers based on the first result object
  Object.keys(data[0]).forEach(key => {
    const th = document.createElement('th');
    th.textContent = key;
    headerRow.appendChild(th);
  });
  table.appendChild(headerRow);
  
  // Create rows for each data item
  data.forEach(item => {
    const row = document.createElement('tr');
    Object.values(item).forEach(value => {
      const td = document.createElement('td');
      td.textContent = value;
      row.appendChild(td);
    });
    table.appendChild(row);
  });
  
  document.getElementById('results').appendChild(table);
}

Using the Response with Visualization Libraries

You can easily integrate Infactory API responses with popular visualization libraries:

Chart.js Example

async function displayChart() {
  const response = await fetch('https://api.infactory.ai/v1/queries/average_by_category', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer YOUR_API_KEY'
    },
    body: JSON.stringify({
      category: "position",
      metric: "height"
    })
  });
  
  const responseData = await response.json();
  const data = responseData.data;
  
  const ctx = document.getElementById('myChart').getContext('2d');
  new Chart(ctx, {
    type: 'bar',
    data: {
      labels: data.map(item => item.position),
      datasets: [{
        label: 'Average Height by Position',
        data: data.map(item => item.average_height),
        backgroundColor: 'rgba(75, 192, 192, 0.2)',
        borderColor: 'rgba(75, 192, 192, 1)',
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        y: {
          beginAtZero: true
        }
      }
    }
  });
}

Streaming Responses

For queries that might return large datasets, Infactory supports streaming responses:

async function streamResults() {
  const response = await fetch('https://api.infactory.ai/v1/query', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer YOUR_API_KEY',
      'Accept': 'text/event-stream'
    },
    body: JSON.stringify({
      query: "List all players and their statistics",
      project_id: "your_project_id",
      stream: true
    })
  });
  
  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    
    const chunk = decoder.decode(value);
    // Process each chunk as it arrives
    processChunk(chunk);
  }
}

Best Practices

  • Use the Unified Endpoint for natural language interfaces where users might ask a variety of questions
  • Use Direct Query Endpoints for specific, predefined operations in your application
  • Implement Proper Error Handling to give users helpful feedback when things go wrong
  • Cache Common Results to improve performance and reduce API calls
  • Implement Rate Limiting in your application to avoid hitting API limits
  • Secure Your API Keys and never expose them in client-side code

Next Steps

Now that you know how to deploy queries and integrate with the Infactory API, learn how to explore and test your queries in the Exploring Queries guide.

Was this page helpful?