DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
FREEDOM251

Freedom251 › Blog › CLI

How to configure OpenCode server access, authentication, and allowed origins

By Freedom251 Team · · 9 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use opencode serve for a headless HTTP server, opencode web for the browser interface, or the separate v2 pairing/service workflow for managed remote access. Keep the default 127.0.0.1 binding for a local-only server. If another device must connect, bind to 0.0.0.0, set OPENCODE_SERVER_PASSWORD, and add only the browser origins you actually need. CORS allows a browser origin; it does not replace authentication.

The standalone server documentation lists port 4096 and hostname 127.0.0.1 as defaults. The Web command chooses an available port while still binding locally by default. The v2 CLI documents a different pairing model, so do not mix its session and remote-access behavior with standalone-server defaults.

As an Amazon Associate I earn from qualifying purchases.

Choose the OpenCode access mode first

OpenCode has three documented ways to provide access. They overlap in purpose but have different defaults and security controls.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Mode Command or flow Documented default behavior Best fit
Standalone HTTP server opencode serve Port 4096, hostname 127.0.0.1, mDNS disabled, and no additional CORS origins Headless access from an OpenCode client or another local integration
Browser interface opencode web Binds to 127.0.0.1, selects an available port, and opens the application in the default browser Interactive use in a local browser
v2 CLI pairing/service opencode pair, opencode pair --remote, and opencode service set One-time pairing links expire after five minutes; paired sessions last 30 days Pairing a client locally or arranging documented remote access

The server page describes opencode serve as a foreground, headless HTTP server with an OpenAPI endpoint at /doc. The Web page describes a browser application rather than a second name for the standalone service. Treat those as separate launch paths when diagnosing ports, bindings, or browser errors.

#1 Best Overall
Sale
TOZO A1 Wireless Earbuds Bluetooth 5.3 Light Weight in Ear Headphones | IPX5 Waterproof,2 Mics for AI Calls,Immersive Premium Sound Bass Headset with Charging Case,32 EQ via App Black
  • [Ultra-Lightweight Ear Buds Designed for Small Ears] Each earbud weighs only 3.7g and features a compact, ergonomic in-ear design made especially for small ears. Secure, low-profile, and comfortable for workouts, all-day wear or overnight sleeping.The ultra-slim profile sits flush in your ear with no hard edges poking or rubbing, so you can even sleep on your side without discomfort.
  • [Immersive Stereo Sound with TOZO OrigX Technology] TOZO OrigX tuning delivers clear vocals, balanced mids, and natural stereo sound for music, podcasts, and videos.
  • [Long Battery Life for Daily Use] Get up to 7 hours of playtime on a single charge, with up to 32 hours total using the charging case—ideal for workdays, commuting, and extended listening sessions.
  • [Bluetooth 5.3 & Stable Connection] Bluetooth 5.3 provides fast pairing, stable wireless performance, and reduced dropouts as you move around home or office.
  • [Deep Bass with Clear Vocals] High-performance drivers produce punchy bass while keeping vocals clean and detailed for everyday listening.

Understand the security boundary

A process listening on 127.0.0.1 is intended for the same machine. Changing the hostname to 0.0.0.0 makes the service reachable through the machine’s network interfaces, which changes the risk profile. OpenCode’s Web documentation explicitly warns that if OPENCODE_SERVER_PASSWORD is not set, the server is unsecured.

HTTP Basic authentication uses a username and password. Set OPENCODE_SERVER_PASSWORD to require a password. The username defaults to opencode; set OPENCODE_SERVER_USERNAME when a different username is required. The password setting applies to both opencode serve and opencode web.

  • Keep 127.0.0.1 when only local processes or the local browser need access.
  • Use 0.0.0.0 only when another interface or device must reach the service.
  • Set the password before exposing a network interface.
  • Permit only the specific browser origins needed for your client.
  • Remember that CORS controls which browser origins may make requests; it is not an authentication mechanism.

Run a local standalone server

Start with the documented defaults

  1. Open a terminal on the machine where OpenCode is installed.
  2. Run opencode serve.
  3. Use the documented local address, 127.0.0.1, and default port 4096 when connecting from a local client.
opencode serve

The process stays in the foreground, so its terminal remains the place to observe startup and runtime errors. The server publishes its OpenAPI document at /doc. With the documented defaults, a local browser or HTTP client can request http://127.0.0.1:4096/doc to inspect that endpoint.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When localhost is the right choice

Use the default binding for a personal workstation, a local development client, or a browser running on the same computer. It avoids making the service reachable through the LAN while preserving the normal server API. You do not need a CORS entry merely because the service has an OpenAPI endpoint; add an origin only when a browser client is actually served from a different origin.

Expose the server on a network safely

Bind to all interfaces with a password

The documented LAN pattern sets a password and changes the hostname to 0.0.0.0:

OPENCODE_SERVER_PASSWORD='replace-with-a-secret' 
OPENCODE_SERVER_USERNAME='opencode' 
opencode serve --hostname 0.0.0.0

OPENCODE_SERVER_USERNAME is optional when the default username opencode is acceptable. Replace the example password with a unique secret; do not publish the example value. The same password environment variable is used by opencode web if that is the process you expose.

Rank #2
Sale
Smart Watch for Men Women Compatible with iPhone Samsung Android Phone 1.69 inch Full Touch Screen IP68 Waterproof Bluetooh Fitness Tracker Heart Rate/Sleep Monitor (Black)
  • 【IP68 Waterproof Multifunctinal Smart Watch】Torjalph fitness activity tracker smart watch is IP68 waterproof technology ,you can wear it when swimming,handwashing,it's rain-proof which means that you perform multiple exercise modes outside without worrying about sweat or rain corroding your fitness smart watch,and this watch equipped with a 230 mAh large capacity battery magnetic charging ,and this sports activity tracker watch can be used 5-7 days If fully charged.
  • 【Larger 1.83'' Full Screen Watch & Multi-dial Selection】1.83 inch full HD color touch screen smart watch for men / women provides excellent 240*284 resolution image quality ,This Bluetooth smart watch offers a better touch experience and remarkable immersion and ,more than 200 watch faces for you to choose from App"Da Fit ", you can change the dial style according to your mood to wear keeping the world on your wrist
  • 【Accurate Activity Tracker Smart Watch 】sports watch activity tracker uses sweat and dust resistant breathable silicone strap, our Fitness Tracker Smart Watch for Android iOS Phones supports 12 sports modes,walking,running,bicycle,swimming,badminton,basketball,football,rope skipping. This bluetooth calls smart watch for men women is lightweight and stylish, bringing more convenient and relaxing sports experience while also providing more protection.
  • 【Your 24 Hour Personal Daily Health Monitor】Fitness watch for women men full touch screen IP67 waterproof bluetooth smartwatch for Android iOS Phones with heart rate blood pressure monitor multifunction Andriod smart watch with steps counter sleep monitor sports fitness watch, monitoring blood oxygen saturation allows you to understand your physical condition in real time to track fitness progress ,to know your health status in time.
  • 【More Functions Bluetooth Call Smart Watch】This fitness activity smart watch full screen for iPhone and Android phones includes a bunch of new features including:The bluetooth smart watch can hang up the phone, receive and display WhatsAPP /Text /Facebook /Twitter /Messenger reminder.Support sedentary reminder, alarm clock, stopwatch, timer,weather information,music control, remote camera control compatible with Android 5.0 and above or iOS 9.0 and above smartphones.

Use a different port when required

The standalone server documents 4096 as its default port. If another process already uses that port, configure a different port using the server’s documented command or configuration setting, then make sure the client connects to that same port. The Web command does not promise a fixed port: it chooses an available one when it starts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the address from another device

Use the host machine’s network address and the configured port from the other device. A connection that works at 127.0.0.1 but fails through the machine’s LAN address usually indicates a binding, firewall, routing, or origin issue rather than an OpenCode API problem. Confirm the process was started with a network-facing hostname and that the password is set before investigating browser policy.

Configure the browser interface

Start Web locally

opencode web

The Web documentation says this command binds to 127.0.0.1 by default, selects an available port, and opens the application in the default browser. This is the simplest local workflow because the browser and server run on the same host and the command handles the browser launch.

Use Web from another device

When the browser is not on the same host, combine a network-facing hostname with password authentication. The password warning applies here exactly as it does to the standalone server: leaving OPENCODE_SERVER_PASSWORD unset leaves the server unsecured.

OPENCODE_SERVER_PASSWORD='replace-with-a-secret' 
opencode web --hostname 0.0.0.0

The Web command’s port may be selected dynamically. Record the port printed or opened by the command and use that value when configuring the remote browser or client.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Allow browser origins with CORS

What an allowed origin does

A browser identifies a page by its origin, which includes its scheme, host, and port. If a browser application is served from a different origin than the OpenCode server, the server must allow that exact origin for cross-origin requests. The CORS setting is an allowlist for additional browser origins. It does not disable HTTP Basic authentication and it does not make an unprotected network binding safe.

Rank #3
New Bee Guitar Cable 10ft Electric Instrument Bass AMP Cord | 6.35mm Jack, Durable Design, Stage and Home Studio Use, Low Noise Shielded Electric Cord for Bass, Keyboard, Amp, Mixer
  • [Low Noise High Fidelity] New bee guitar cord with 22AWG oxygen-free-copper center conductors provides enhanced signal transfer, the PVC insulation material and the high-density stranded copper double shield provide noise rejection for quiet operation
  • [Ideal for Choice] New bee 1/4 straight and right angle metal connectors, ensures a secure connection in any instrument, pedal, or amp jack. L shape great fro on stage guitar stands
  • [Wide Compatible] Suitable for mixer, power amplifier, microphone, effector, decoder, equalizer, electoronic organ, electric guitar, electric bass and other electronic equipments. Widely used in the installation of smart home, karaoke speaker, professional stage audio, etc
  • [Easy Disassemble] Making incidental repairs quick and easy! Very nice, flexible cable
  • [Dont Hesite to Order] 24 hours customer services and professional technology team are standing by

Add origins on the command line

The server documentation supports repeated --cors options. Supply one option for each origin:

opencode serve 
  --cors http://localhost:5173 
  --cors https://app.example.com

Only add origins that correspond to real browser clients. Keep the scheme and port exactly as the browser uses them: http://localhost:5173 and https://app.example.com are different origins. If your development server changes ports, update the allowlist instead of assuming that one entry covers every port.

Put server settings in opencode.json

The Web documentation provides a server configuration object with port, hostname, mDNS, and CORS settings:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
{
  "server": {
    "port": 4096,
    "hostname": "0.0.0.0",
    "mdns": true,
    "cors": ["https://example.com"]
  }
}

This example exposes the service on network interfaces, enables mDNS, and allows the listed HTTPS origin. Adapt each value to your deployment rather than copying the example blindly.

Know which setting wins

In the documented Web flow, command-line flags take precedence over values in the configuration file. If a file says hostname is 0.0.0.0 but the command supplies a different hostname, the command-line value is the effective one. The same precedence rule matters for port, mDNS, and CORS flags. When behavior does not match the file, inspect the launch command first.

Enable local-network discovery with mDNS

The Web command can advertise the service through mDNS:

Rank #4
BSIMB 15.6" Digital Family Calendar & Chore Chart, No Subscription Fees
  • Digital Family Calendar: BSIMB digital calendar wall touch screen puts an end to your chaotic life. Designed for busy families, the digital calendar keeps you organised and in step with your family. Assign different colours and tasks to each family member to make family scheduling easier and more intuitive.
  • Easy Setup and Auto-Sync: Plug in, connect to WLAN and auto-syn your electric calendar in minutes. Digital wall calendar compatible with Google calendar, iCloud calendar, Outlook calendar, Cozi calendar and Yahoo calendar. Add events directly on the device or through the ecalendar app. Electronic calendar stays current whether you're at work or traveling—no manual updates needed.
  • All-In-One Home Assistant: Smart calendar builds habits and encourage independence with personalised chores. Display daily meal plan to reduce the daily ‘What's for dinner?’ stress. To-do lists keep track of everything you want. Turning chores into valuable achievements with stars and rewards, making life easier and more enjoyable.
  • Manage Anytime Anywhere: BSIMB electronic calendar for family stay connected with the powerful ecalendar app. Access and manage your digital wall calendar anytime, anywhere with free features like stars & rewards, photo screensaver, chore charts, meals plans, to-do lists etc. Electronic planner paid features like Magic Import tailor advanced tools for users with special needs.
  • Stay in Touch with Loved Ones: Share photos or videos from your phone to electronic wall calendar with app, instantly update your life moment, and keep in touch with your family, especially those who are far away; you can also surprise your smart calendar digital wall by sending him a wish card; it's also great for keeping tech-illiterate elders in touch with the youngsters.
opencode web --mdns

The Web documentation says that enabling mDNS automatically sets the hostname to 0.0.0.0 and advertises the service as opencode.local. That automatic hostname change means mDNS is not a localhost-only setting. Use it only when local-network discovery is wanted, and still configure a password for any network-reachable service.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To use a custom mDNS name, provide --mdns-domain with the domain supported by your OpenCode version. Keep the custom name distinct from ordinary browser origins: discovery helps a client find the host, while CORS determines whether a browser origin may call it.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use the v2 pairing and service flow

The current v2 CLI documentation describes a separate pairing/service approach. It is not simply another spelling of opencode serve, so apply its timing and remote-access rules only to this flow.

Local pairing

Use opencode pair for the documented local pairing workflow. Pairing links are one-time links and expire after five minutes. Once a client is paired, the documented session lifetime is 30 days.

Remote pairing

Use opencode pair --remote when the v2 flow is intended to provide remote access. The first remote use prompts for confirmation before exposing the server to the internet. The documentation states that access requires the server password, so configure that password before approving remote exposure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Change service settings

The v2 CLI documents opencode service set for changing service properties such as hostname, port, password, and CORS. Use the option names and syntax shown by the version-specific help for your installed CLI. Do not assume that a setting changed through this service command has the same startup defaults as the standalone serve command.

Understand password changes

Changing the server password signs every existing v2 session out. Plan a re-pairing or re-login for clients that must continue working after the change.

Choose a configuration by scenario

Scenario Binding Authentication Origin or discovery setting
Local headless client 127.0.0.1 Set a password if any other process or user could reach the service No additional CORS; mDNS remains disabled
Local browser with opencode web 127.0.0.1 Use OPENCODE_SERVER_PASSWORD when the server is not strictly private Usually no extra origin
Browser on another host 0.0.0.0 OPENCODE_SERVER_PASSWORD, with optional username override Add each browser origin with repeated --cors flags or server.cors
LAN discovery mDNS sets the hostname to 0.0.0.0 Set a password before enabling discovery opencode web --mdns, optionally with --mdns-domain
v2 remote pairing Configured through the v2 service flow Password required; changing it signs out all sessions opencode pair --remote; one-time links expire after five minutes

Troubleshoot the common failure modes

The remote device cannot connect

  • Check whether the process is still bound to 127.0.0.1. A localhost binding cannot accept connections addressed to the host’s LAN interface.
  • Start the service with the documented network-facing hostname, normally --hostname 0.0.0.0, or set the equivalent server configuration.
  • Confirm the client is using the actual port. Standalone serve documents 4096; web chooses an available port.
  • Check host firewall and network routing rules separately from OpenCode settings.

The browser reports a CORS error

  1. Copy the browser application’s complete origin, including http or https and the port.
  2. Add that exact value with --cors or to the server.cors array.
  3. If both a config file and command flags exist, inspect the flags because they take precedence.
  4. Restart the process after changing startup settings, then retry the browser request.

Do not respond to a CORS error by removing authentication. CORS and HTTP Basic authentication solve different problems.

The browser asks for credentials or rejects them

Use the configured username and password. The default username is opencode unless OPENCODE_SERVER_USERNAME changes it. If the password was changed in the v2 service flow, existing sessions are invalidated and must authenticate again.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

mDNS does not behave as expected

Remember that --mdns changes the hostname to 0.0.0.0 and advertises opencode.local. Test the service using its advertised name only on a network where mDNS discovery is available. For a different name, use --mdns-domain and confirm that the client supports the custom domain.

A configuration file appears to be ignored

Look for a command-line override first. The documented precedence rule gives flags priority over file values. Compare the effective hostname, port, mDNS state, and CORS entries with the command that actually launched OpenCode.

Deployment checklist

  • Identify whether the process is serve, web, or the v2 service/pairing flow.
  • Keep the hostname at 127.0.0.1 unless another device needs access.
  • When exposing a network interface, set OPENCODE_SERVER_PASSWORD before starting the process.
  • Set OPENCODE_SERVER_USERNAME only when the default opencode username should change.
  • Record the effective port: 4096 for the documented standalone default, or the available port selected by opencode web.
  • Add only exact browser origins, repeating --cors for multiple origins or listing them under server.cors.
  • Check command-line flags for overrides to opencode.json.
  • Enable mDNS only when local discovery is useful, remembering that it makes the hostname 0.0.0.0.
  • For v2 remote pairing, approve internet exposure deliberately, keep the password configured, and expect one-time links to expire after five minutes.
  • After a v2 password change, re-authenticate every client because all existing sessions are signed out.

Which documentation to consult when behavior differs

The standalone Server and Web pages were last updated October 13, 2026, while the pairing instructions are version-specific v2 CLI documentation. Use the Server page for serve defaults and the /doc endpoint, the Web page for browser startup, mDNS, CORS, and configuration precedence, and the v2 page for pairing, service settings, remote prompts, and session duration. This separation prevents a v2 pairing rule from being mistaken for a standalone-server default.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

CLICORSHTTP Basic AuthenticationmDNSOpenCodeserver configuration

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.