Skip to content
PrivateAI
← Back to Home
Local AI Setup

Ollama Local API: Connect to VS Code, Python, and n8n for Private AI Automation

9 min read min readBy PrivateAI Team

Last updated: 2026-06-15

The Short Answer: Ollama Already Has an API

You do not need an API key. You do not need a cloud account. The moment you run ollama serve, you have a fully functional REST API listening on http://localhost:11434 — compatible with the OpenAI spec, which means almost every AI tool you already use can point at it instead.

This guide shows exactly how to wire that API into three environments where it matters most: VS Code (for AI-assisted coding without sending your code to the cloud), Python (for building your own automation scripts), and n8n (for no-code/low-code workflows that stay on your machine).

Everything here runs locally. Nothing leaves your network unless you explicitly route it out.


What Ollama's API Actually Looks Like

Before connecting anything, run this in your terminal to confirm the server is up:

```bash

curl http://localhost:11434/api/tags

```

You should get JSON listing every model you have pulled. If you see connection refused, run ollama serve in a separate terminal first — on some systems it does not start automatically.

The two endpoints you will use most:

| Endpoint | What it does |

|---|---|

| POST /api/generate | Single-turn completions |

| POST /api/chat | Multi-turn conversations with message history |

| GET /api/tags | List available models |

Ollama also exposes an OpenAI-compatible shim at /v1/chat/completions. This is the one most third-party tools expect, and it is what makes the integrations below so clean.


VS Code: AI Code Assistance With Zero Data Leakage

The fastest path is the Continue extension. It is open source, model-agnostic, and built specifically for local LLM workflows.

Install and configure:

  1. Install the Continue extension from the VS Code marketplace
  2. Open the Continue config file — click the gear icon in the Continue sidebar, or open ~/.continue/config.json directly
  3. Replace the default model block with:

```json

{

"models": [

{

"title": "Llama 3.2 (Local)",

"provider": "ollama",

"model": "llama3.2",

"apiBase": "http://localhost:11434"

}

],

"tabAutocompleteModel": {

"title": "Qwen2.5-Coder (Local)",

"provider": "ollama",

"model": "qwen2.5-coder:7b",

"apiBase": "http://localhost:11434"

}

}

```

  1. Save the file — Continue reconnects automatically

For autocomplete, qwen2.5-coder significantly outperforms general-purpose models on code tasks. Pull it with ollama pull qwen2.5-coder:7b before enabling tab completion.

What you get: inline code suggestions, chat-based refactoring, /edit commands that rewrite selected blocks, and /cmd that translates your intent into shell commands — all running on your own GPU or CPU, with no request ever leaving your machine.

If you want a hardware upgrade to make local models feel instant, a dedicated GPU with at least 12GB VRAM (for 7B-13B models) is the single biggest performance lever. The NVIDIA RTX 4070 Super hits the sweet spot for local LLM inference without going into workstation pricing.

Affiliate Disclosure: This article may contain affiliate links. If you make a purchase through these links, we may earn a small commission at no extra cost to you. We only recommend products we genuinely believe in. This helps support our work and allows us to continue providing free content.


Python: Direct API Calls for Custom Scripts

Python integration has two paths. Use the ollama library for local scripts where you control the environment. Use the openai library when you are dropping Ollama into a codebase that already uses OpenAI — you only change the base_url.

Path 1: The ollama Python Library

```bash

pip install ollama

```

Single-turn completion:

```python

import ollama

response = ollama.generate(

model="llama3.2",

prompt="Summarize this document in 3 bullet points: [your text here]"

)

print(response["response"])

```

Multi-turn chat:

```python

import ollama

messages = [

{"role": "user", "content": "What is the capital of France?"},

]

response = ollama.chat(model="llama3.2", messages=messages)

reply = response["message"]["content"]

messages.append({"role": "assistant", "content": reply})

messages.append({"role": "user", "content": "What is its population?"})

response = ollama.chat(model="llama3.2", messages=messages)

print(response["message"]["content"])

```

The library handles streaming too — pass stream=True and iterate over the response to get tokens as they generate rather than waiting for the full output.

Path 2: Drop-in OpenAI Replacement

If you have existing Python code that calls OpenAI, this is a two-line swap:

```python

from openai import OpenAI

client = OpenAI(

base_url="http://localhost:11434/v1",

api_key="ollama" # Required by the library, not actually checked

)

response = client.chat.completions.create(

model="llama3.2",

messages=[{"role": "user", "content": "Write a regex for US phone numbers"}]

)

print(response.choices[0].message.content)

```

The api_key value is ignored by Ollama — pass any non-empty string. This approach lets you build scripts that switch between local and cloud models by changing one variable, which is useful when you want local for development and a cloud fallback for production edge cases.

Practical Script: Batch Summarizer

Here is a real-world pattern — process a folder of text files and write summaries alongside them:

```python

import ollama

from pathlib import Path

SOURCE_DIR = Path("./documents")

MODEL = "llama3.2"

for txt_file in SOURCE_DIR.glob("*.txt"):

content = txt_file.read_text()

response = ollama.generate(

model=MODEL,

prompt=f"Summarize this in 2-3 sentences:\n\n{content}"

)

summary_path = txt_file.with_suffix(".summary.txt")

summary_path.write_text(response["response"])

print(f"Summarized: {txt_file.name}")

```

Run this overnight on a folder of PDFs (after extracting text with pdfplumber) and wake up to a processed archive — no API costs, no data uploaded.


n8n: Visual Workflows With Local AI Nodes

n8n has native Ollama support as of late 2024. If you are running a self-hosted n8n instance (the correct choice for privacy), you connect through the credentials system rather than hardcoding URLs.

Prerequisites:

  • n8n running locally or on a private server (Docker is the cleanest path: docker run -p 5678:5678 n8nio/n8n)
  • Ollama running on the same machine or reachable via local network

Add Ollama credentials in n8n:

  1. Go to Settings → Credentials → New Credential
  2. Search for "Ollama"
  3. Set the base URL to http://localhost:11434 (or your machine's local IP if n8n runs in Docker: http://host.docker.internal:11434 on Mac/Windows)
  4. Save — no API key needed

Build a basic automation workflow:

The most immediately useful pattern is a webhook-triggered summarizer:

  1. Webhook node — triggers the workflow when called with a POST request containing {"text": "..."}
  2. Ollama Chat Model node — configure with your credential, select model
  3. AI Agent or Basic LLM Chain node — connect the Ollama model, set your system prompt
  4. Respond to Webhook node — returns the model's output as the HTTP response

Once this is running, you can call it from anywhere on your network — a browser bookmarklet, a mobile shortcut, another automation — and get AI-processed text back without any external API involvement.

More advanced n8n patterns with Ollama:

  • Email triage: Trigger on new email → Ollama classifies priority and intent → route to folders or draft replies
  • Document Q&A: Load a PDF → chunk it → store embeddings locally (use n8n's vector store nodes with a local embedding model) → query against new inputs
  • Scheduled reports: Pull data from an API or database on a schedule → Ollama writes the narrative summary → send to Slack or email

For the embedding-based workflows, pull a dedicated embedding model: ollama pull nomic-embed-text. It is small (274MB) and purpose-built for retrieval tasks.


Connecting Across Your Network (Optional)

By default, Ollama only listens on localhost. If you want to reach it from another machine — say, a separate server running n8n — set this environment variable before starting Ollama:

```bash

OLLAMA_HOST=0.0.0.0:11434 ollama serve

```

Then access it at http://[your-machine-ip]:11434 from other devices on the same network. Do not expose this port to the public internet without a reverse proxy and authentication layer in front of it.

If you need remote access from outside your network, route it through Tailscale rather than opening firewall ports. Tailscale gives every device a stable private IP (100.x.x.x range) and encrypts traffic between them — your Ollama instance becomes reachable at a consistent address from anywhere, without touching your router.

Affiliate Disclosure: This article may contain affiliate links. If you make a purchase through these links, we may earn a small commission at no extra cost to you. We only recommend products we genuinely believe in. This helps support our work and allows us to continue providing free content.


Model Selection by Use Case

Not every task needs the same model. Running a smaller, faster model for simple classification saves time:

| Task | Recommended Model | Why |

|---|---|---|

| Code completion | qwen2.5-coder:7b | Trained on code, fast on CPU |

| General chat / writing | llama3.2:3b or llama3.2 | Solid reasoning, widely tested |

| Document summarization | mistral:7b | Strong at instruction following |

| Embeddings / RAG | nomic-embed-text | Purpose-built, tiny footprint |

| Long context tasks | qwen2.5:14b | 128K context window |

Pull only what you need. Each model takes 4-8GB of disk space. Keep your active set small and pull others on demand.


Start With One Connection, Expand From There

The Ollama API is intentionally minimal, which is why it plugs into so many tools without adapters or translation layers. The OpenAI compatibility shim does most of the work.

Pick one of the three integrations above based on what you actually need today. Get it working end-to-end before adding the next one. The VS Code setup has the fastest payoff for developers — within fifteen minutes of installing Continue, you stop sending code to the cloud and the workflow feels identical to GitHub Copilot.

The Python path is the right starting point if you have automation scripts in progress or want to build something custom. The n8n integration requires the most setup but unlocks the broadest range of non-code automation once it is running.

All three share the same API. Master the connection pattern once, and the rest is just configuration.


Stay private. Every guide we publish is built around tools that keep your data yours. Subscribe below for weekly breakdowns of local AI setups, self-hosted tools, and privacy-first automation workflows — no cloud account required to read them.

Subscribe to PrivateAI Weekly →


Last updated: 2026-06-15

```