> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lindy.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Code

<div style={{ display: 'flex', justifyContent: 'center', margin: '2rem 0' }}>
  <div className="video-card">
    <video src="https://mintcdn.com/lindyai/QWdJNkViLsD1veyH/lindy-brand-assets/run-code-overview.mp4?fit=max&auto=format&n=QWdJNkViLsD1veyH&q=85&s=4f1d198e95fbf371f2ffad460870a91f" width="600" autoPlay muted loop playsInline style={{ display: 'block', width: '100%', borderRadius: '16px' }} data-path="lindy-brand-assets/run-code-overview.mp4" />
  </div>
</div>

## Quick Overview

The Code action lets you run custom Python or JavaScript code directly within your agent workflows. It's ideal for tasks like data manipulation, data analysis, API calls, complex calculations, or conditional logic.

Key benefits:

* **Multi-language support** - Write in Python or JavaScript
* **Variable integration** - Use outputs from previous steps as variables
* **Flexible output** - Return results, printed text, or error messages
* **Rich libraries** - Access to popular data science and utility libraries

## Action Configuration

<Frame>
  <img src="https://mintcdn.com/lindyai/VHBHJwXMXFd-cbnu/lindy-brand-assets/code-language-choices.png?fit=max&auto=format&n=VHBHJwXMXFd-cbnu&q=85&s=1b49533e8f307eed0eb1dcf69e0d1802" alt="Code action configuration showing language selection, variables, and code editor" width="2864" height="1492" data-path="lindy-brand-assets/code-language-choices.png" />
</Frame>

### Language Selection

Choose between Python and JavaScript based on your needs:

* **Python** - Best for data analysis, scientific computing, and complex calculations
* **JavaScript** - Ideal for web APIs, JSON manipulation, and async operations

### Variables

<Frame>
  <img src="https://mintcdn.com/lindyai/VHBHJwXMXFd-cbnu/lindy-brand-assets/code-variables.png?fit=max&auto=format&n=VHBHJwXMXFd-cbnu&q=85&s=b02eea4c4f9b91a08e705542c1d307c7" alt="Variables configuration showing name and value fields with output references" width="3114" height="1834" data-path="lindy-brand-assets/code-variables.png" />
</Frame>

Set up variables to pass data from previous steps into your code:

* **Name** - The variable name you'll use in your code
* **Value** - Set to output references from previous steps
* **Type** - Variable values are always passed as strings

<Warning>
  All variable values are passed as strings. Convert to appropriate types (int, float, etc.) within your code as needed.
</Warning>

### Code Editor

<Frame>
  <img src="https://mintcdn.com/lindyai/VHBHJwXMXFd-cbnu/lindy-brand-assets/code-editor.png?fit=max&auto=format&n=VHBHJwXMXFd-cbnu&q=85&s=5cbcc8a2a1a471cb26d8f416289fe5be" alt="Code editor showing Python code with variable usage and function definition" width="3146" height="1846" data-path="lindy-brand-assets/code-editor.png" />
</Frame>

Write your code using the variables defined in the previous step. The code editor supports:

* Syntax highlighting
* Auto-completion
* Error detection
* Multi-line code blocks

## Reference Code Output

<Frame>
  <img src="https://mintcdn.com/lindyai/VHBHJwXMXFd-cbnu/lindy-brand-assets/code-variables-reference.png?fit=max&auto=format&n=VHBHJwXMXFd-cbnu&q=85&s=da02ea1f0dcfa94c2c3f454e79bdc89d" alt="Output references showing result, text, and stderr options in subsequent steps" width="1389" height="1542" data-path="lindy-brand-assets/code-variables-reference.png" />
</Frame>

The results of your code action can be used in subsequent steps with three output references:

* **result** - What your code returns (using `return` statement)
* **text** - Any text printed during execution (stdout)
* **stderr** - Standard error buffer for debugging

<Tip>
  If you want to see what the example code output looks like within the test panel or task view, you can click into the code action and see the JSON for all the outputs, so you can use them appropriately.
</Tip>

## Available Libraries

### Python Libraries

<AccordionGroup>
  <Accordion title="Data Science & Analytics">
    * **pandas** (v1.5.3) - Data manipulation and analysis
    * **numpy** (v1.26.4) - Numerical computing
    * **scipy** (v1.12.0) - Scientific computing
    * **scikit-learn** (v1.4.1.post1) - Machine learning
    * **matplotlib** (v3.8.3) - Plotting and visualization
    * **seaborn** (v0.13.2) - Statistical visualization
    * **plotly** (v5.19.0) - Interactive plotting
    * **bokeh** (v3.3.4) - Web-based visualization
  </Accordion>

  <Accordion title="Web & API">
    * **requests** (v2.26.0) - HTTP requests
    * **aiohttp** (v3.9.3) - Async HTTP client/server
    * **urllib3** (v1.26.7) - HTTP client library
    * **beautifulsoup4** (v4.12.3) - Web scraping
  </Accordion>

  <Accordion title="Text & NLP">
    * **nltk** (v3.8.1) - Natural language processing
    * **textblob** (v0.18.0) - Text processing
    * **spacy** (v3.7.4) - Advanced NLP
    * **gensim** (v4.3.2) - Topic modeling
  </Accordion>

  <Accordion title="File & Data Processing">
    * **openpyxl** (v3.1.2) - Excel file handling
    * **xlrd** (v2.0.1) - Excel file reading
    * **python-docx** (v1.1.0) - Word document processing
    * **xarray** (v2024.2.0) - N-dimensional arrays
  </Accordion>

  <Accordion title="Media & Image Processing">
    * **opencv-python** (v4.9.0.80) - Computer vision
    * **scikit-image** (v0.22.0) - Image processing
    * **imageio** (v2.34.0) - Image I/O
    * **librosa** (v0.10.1) - Audio analysis
    * **soundfile** (v0.12.1) - Audio file I/O
  </Accordion>

  <Accordion title="Utilities">
    * **pytz** (v2024.1) - Timezone handling
    * **joblib** (v1.3.2) - Parallel computing
    * **tornado** (v6.4) - Web framework
    * **pytest** (v8.1.0) - Testing framework
    * **sympy** (v1.12) - Symbolic mathematics
  </Accordion>
</AccordionGroup>

## Use Cases

<AccordionGroup>
  <Accordion title="Data Analysis">
    Process CSV files, calculate statistics, generate reports from your data sources
  </Accordion>

  <Accordion title="API Integration">
    Call external APIs, parse responses, and format data for your workflows
  </Accordion>

  <Accordion title="Complex Calculations">
    Perform mathematical operations, financial calculations, or scientific computations
  </Accordion>

  <Accordion title="Data Transformation">
    Convert between data formats, clean datasets, or restructure information
  </Accordion>

  <Accordion title="Conditional Logic">
    Implement complex decision trees and branching logic beyond simple conditions
  </Accordion>
</AccordionGroup>

## Best Practices

<AccordionGroup>
  <Accordion title="Error Handling">
    Use try-catch blocks to handle potential failures and return meaningful error messages
  </Accordion>

  <Accordion title="Variable Validation">
    Check variable types and values before processing to avoid runtime errors
  </Accordion>

  <Accordion title="Efficient Code">
    Optimize for performance, especially with large datasets to avoid timeouts
  </Accordion>

  <Accordion title="Clear Returns">
    Return structured data that's easy to use in subsequent workflow steps
  </Accordion>

  <Accordion title="Documentation">
    Comment your code for future maintenance and debugging
  </Accordion>
</AccordionGroup>

<Tip>
  Always handle errors gracefully in your code. Use try-catch blocks and return meaningful error messages.
</Tip>

<Warning>
  Code execution has timeouts. Keep your code efficient and avoid infinite loops or very long-running operations.
</Warning>

## Next Steps

<CardGroup cols={2}>
  <Card title="Agent Steps" href="/fundamentals/lindy-101/ai-agents" icon="brain-circuit">
    Combine code with AI for powerful data processing
  </Card>

  <Card title="HTTP Request" href="/skills/by-lindy/http-request" icon="globe">
    Make API calls to external services
  </Card>

  <Card title="Knowledge Base" href="/fundamentals/lindy-101/knowledge-base" icon="database">
    Store and retrieve processed data
  </Card>

  <Card title="Conditions" href="/fundamentals/lindy-101/conditions" icon="network-wired">
    Add logic to your code workflows
  </Card>
</CardGroup>
