58API Help Center58API Help Center
User GuideAI ApplicationsAPI DocumentationHelp & Support

Jupyter - Interactive AI programming and data analysis

Configure 58API in JupyterLab or Jupyter Notebook with one click, and use the Python SDK to complete text generation, streaming output, model query, and image generation.

Scope of application

This tutorial applies to JupyterLab and Jupyter Notebook. After the configuration is completed, you can directly call 58API's text, image and other models in the Python cell, and save the code, description and output in the same Notebook.

The steps in this article are based on Jupyter official installation instructions、JupyterLab Notebook Operation Instructionsand IPython startup file descriptiontidy. Screenshots based on JupyterLab 4.6.3.

Preparation before use

  1. Log in 58API, create an API Key in the console, and confirm that the balance is sufficient.
  2. Native installation of Python 3.10 or higher (JupyterLab 4.6, Notebook 7.6, and the latest OpenAI Python SDK all require Python ≥ 3.10).
  3. Install JupyterLab or Jupyter Notebook.
  4. Install the latest version of OpenAI Python SDK.

Install Jupyter

JupyterLab

JupyterLab is a new interface with more complete functions and is recommended for first-time users:

python -m pip install --upgrade jupyterlab openai
jupyter lab

Classic Jupyter Notebook

python -m pip install --upgrade notebook openai
jupyter notebook

If using Conda, you can do this from conda-forge Install JupyterLab and then install OpenAI SDK in the same environment:

conda install -c conda-forge jupyterlab
python -m pip install --upgrade openai

After the installation is complete, run jupyter lab. After entering the launcher, select Python 3 (ipykernel) in the Notebook area to create a Python Notebook:

Select the Python 3 kernel in the JupyterLab launcher to create a Notebook

Confirm installation into current kernel

Jupyter may use multiple Python environments simultaneously. Open Notebook and run import sys; print(sys.executable), you can confirm the Python path used by the current kernel.

One-click configuration 58API

The script asks for the API base address and API Key, and creates a Jupyter/IPython startup file. Every time you start the Python kernel in the future, it will be automatically set:

OPENAI_BASE_URL=https://api.58api.net/v1
OPENAI_API_KEY=your_58API_api_key

Please run the script in terminal

The one-click script requires interactive input. Please run it in the system terminal (PowerShell for Windows) or the "Terminal" of JupyterLab. Do not use it in Notebook cells. ! implement.

Windows

Open PowerShell and execute:

iex (irm 'https://58api.net/helper/jupyter-setup.ps1')

Follow the prompts:

  1. Just press Enter when the API base address prompt appears (the default value in the prompt is https://api.58api.net), the script will automatically complete it as https://api.58api.net/v1;
  2. Paste the 58API API Key; no characters will be displayed on the screen when entering the key;
  3. Once the configuration is complete, fully restart the Jupyter kernel.

The script writes by default:

%USERPROFILE%\.ipython\profile_default\startup\00-58API.py

macOS / Linux

Open a terminal and execute:

curl -fsSL https://58api.net/helper/jupyter-setup.sh | bash

The operation method according to the prompts is the same as in Windows: press Enter directly for the API base address, then paste the 58API API Key, and restart the Jupyter kernel after completion.

The script writes by default:

~/.ipython/profile_default/startup/00-58API.py

If the system is set IPYTHONDIR, the script will use this directory. Profile permissions will be set to read and write only for the current user.

what the script will do

  • autocomplete https:// and /v1;
  • Do not output API Key to the terminal;
  • Only writes to Jupyter/IPython's dedicated startup files;
  • Do not modify Codex, Claude Code or system global API environment variables;
  • Back up the old one before running it again 00-58API.py, and then write the new configuration.

The API Key will be saved in the local configuration file

Configure a trusted device that suits you with one click. don't put 00-58API.py Upload to Git, network disk shared directory or send to others; do not print the full API Key in the Notebook output.

Let the configuration take effect

After the one-click script is completed, select in Jupyter:

  1. Kernel/kernel;
  2. Restart Kernel/Restart the kernel;
  3. Rerun the Notebook cell.

Select Restart Kernel from the Kernel menu in JupyterLab

Simply refreshing the browser page will not necessarily restart the Python kernel, so a "restart kernel" must be performed.

Verify configuration

Create a new Python Notebook and run:

import os

print("API 地址:", os.getenv("OPENAI_BASE_URL"))
print("API Key:", "已配置" if os.getenv("OPENAI_API_KEY") else "未配置")

Normal results should show:

API 地址: https://api.58api.net/v1
API Key: 已配置

Don't run print(os.getenv("OPENAI_API_KEY")), to prevent keys from being saved to Notebook output.

Verify 58API address and API Key status in JupyterLab kernel

first call

Responses API

OpenAI Python SDK will automatically read the API address and API Key set by the one-click script:

from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-6.1-sol",
    input="用三点说明为什么 Jupyter 适合数据分析。",
)

print(response.output_text)

Chat Completions

When compatibility with traditional message formats is required, you can use:

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-6.1-sol",
    messages=[
        {"role": "system", "content": "你是一名数据分析助手。"},
        {"role": "user", "content": "给出一份 CSV 数据清洗检查清单。"},
    ],
)

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

Streaming output

Streaming output is suitable for longer answers and can be displayed while being generated:

from openai import OpenAI

client = OpenAI()

stream = client.responses.create(
    model="gpt-6.1-sol",
    input="写一段包含读取 CSV、缺失值统计和分组汇总的 Python 示例。",
    stream=True,
)

for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)

View available models

from openai import OpenAI

client = OpenAI()
models = client.models.list()

for model in models.data[:30]:
    print(model.id)

The model list will change with account permissions and 58API configuration. When the model in the example is not available, select a currently available model from the returned list.

Generate images in Notebook

The following cells will generate pictures and display them directly. This operation may incur charges:

import base64
from openai import OpenAI
from IPython.display import Image as NotebookImage, display

client = OpenAI()

result = client.images.generate(
    model="gpt-image-2",
    prompt="一张极简风格的数据分析工作台插画,深蓝背景,发光图表,16:9",
    size="1536x1024",
)

image = result.data[0]
if image.b64_json:
    display(NotebookImage(data=base64.b64decode(image.b64_json)))
elif image.url:
    display(NotebookImage(url=image.url))

Different image models support different sizes and parameters. If the returned parameters are incorrect, please adjust them according to the model documentation. size, or first use the drawing workbench to confirm the model capabilities.

Not using one-click scripts

If you only want to use 58API temporarily in the current Notebook and do not save the key locally, you can create a client through secure input:

from getpass import getpass
from openai import OpenAI

client = OpenAI(
    api_key=getpass("58API API Key: "),
    base_url="https://api.58api.net/v1",
)

getpass The input will not be displayed in the cell, but the client is only valid in the current kernel; you need to re-enter after restarting the kernel.

Update or remove configuration

Change API Key

Just re-run the one-click script corresponding to the system. The script will back up the old configuration and write the 58API address and key.

Remove Windows configuration

$dir = if ($env:IPYTHONDIR) { $env:IPYTHONDIR } else { Join-Path $env:USERPROFILE '.ipython' }
Remove-Item (Join-Path $dir 'profile_default\startup\00-58API.py')

Remove macOS/Linux configuration

rm "${IPYTHONDIR:-$HOME/.ipython}/profile_default/startup/00-58API.py"

Restart the Jupyter kernel after removal. after OpenAI() 58API configuration will no longer be used automatically.

FAQ

The installation was successful, but the Notebook prompt did not openai

Usually the Jupyter kernel and the installation command use different Python. First run:

import sys
print(sys.executable)

Then use the current kernel installation in Notebook:

import sys
!{sys.executable} -m pip install --upgrade openai

After the installation is complete, restart the kernel.

Requests are still made to the default API address

Run the "Verify Configuration" cell to confirm OPENAI_BASE_URL for https://api.58api.net/v1. If there is no update, please stop and restart JupyterLab/Notebook completely, then restart the kernel.

Returns 401 Authentication failed

Re-run the one-click script and confirm that the API Key is complete and has no spaces or newlines. Do not use Codex login tokens, Claude account credentials, or keys from other services as 58API API Keys.

Returns 404 or model does not exist

First run "View Available Models" to confirm that the model name is exactly the same as the return value. Model permissions and names may change based on account configuration.

Returns 429 or insufficient balance

Check the account balance and current model price, reduce the number of concurrent requests, and wait for the current limit window to be restored before trying again. Repeatedly running the same billing cell in the notebook creates new requests.

Notebook to share with others

Clear output that contains sensitive content before sharing, and verify that there are no hard-coded API keys in the code. It is recommended to only share and use OpenAI() or getpass() code, do not share this machine 00-58API.py。