Learn › Local AI: Run Models on Your Own Machine › LM Studio's Local API Server

LM Studio's Local API Server

Beginner 🕐 10 min Lesson 8 of 13
What you'll learn
  • Enable the LM Studio local API server and understand what it exposes
  • Test the API with curl
  • Modify a Python script that uses the OpenAI SDK to call LM Studio instead with one line change

LM Studio as a Local API Server

LM Studio is not just a chat interface. It ships a production-capable local API server that listens at http://localhost:1234 and accepts requests in the same format as OpenAI’s and Anthropic’s APIs. Any application built to talk to a cloud AI API can talk to LM Studio instead — with one URL change.

This is significant: it means you can take a Python script, a workflow automation, a VS Code extension, or any other tool that uses the OpenAI SDK, and run it against your local hardware at zero cost per call.

Enabling the Server

In LM Studio, click the Developer tab (the code icon in the left sidebar). You will see a “Local Server” panel. Ensure a model is loaded in the Chat tab, then click Start Server. The status indicator turns green and shows: Server running on http://localhost:1234.

The server stays running until you click Stop or close LM Studio. It serves whichever model is currently loaded in the Chat tab — switching models in the Chat tab updates the model the API uses.

The Endpoints

LM Studio’s server exposes two API families:

OpenAI-compatible (v1): http://localhost:1234/v1/chat/completions — Accepts the same request format as api.openai.com/v1/chat/completions. Works with the OpenAI Python SDK, LangChain, LlamaIndex, and any other tool built for OpenAI.

Anthropic-compatible: http://localhost:1234/v1/messages — Accepts requests in Anthropic’s Messages API format. Works with the Anthropic Python SDK when you point it at the local URL.

Testing with curl

curl http://localhost:1234/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "local-model", "messages": [{"role": "user", "content": "What is GGUF?"}], "stream": false}'

The model name in the request body does not need to match exactly — LM Studio uses whatever model is currently loaded regardless. Use "model": "local-model" as a placeholder.

Swapping Cloud for Local in Python

Here is the minimal change to take an existing OpenAI script and run it against LM Studio:

# Before — calls OpenAI servers from openai import OpenAI client = OpenAI(api_key="sk-...") # After — calls your local LM Studio from openai import OpenAI client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

The api_key value is required by the SDK but ignored by LM Studio. Everything else in your script stays identical — client.chat.completions.create(), streaming, system messages, all of it works the same way.

API Token Authentication

LM Studio 0.4 introduced optional API token authentication for the local server. In the Developer tab you can generate a token and require it on all requests. This is useful if you expose the server on a local network (not just localhost) and want to control which devices can connect. For purely local use on a single machine, you can leave authentication off.

Key takeaways
  • LM Studio's API server runs at localhost:1234 and is compatible with both the OpenAI SDK (v1/chat/completions) and the Anthropic SDK (v1/messages)
  • Swap cloud for local in any OpenAI SDK script by changing base_url to http://localhost:1234/v1 and setting api_key to any non-empty string
  • The server uses whichever model is loaded in the Chat tab — switch models in the UI to change what the API responds with