API Nexus 快速入门指南¶
本指南将帮助你快速上手 API Nexus,支持 Python 与 Flutter 两种开发环境。
准备工作¶
获取 API Key¶
登录 API Nexus 控制台,在密钥管理页面获取你的 API Key。API Key 格式为 nxk-xxxxxxxxxxxxxxxx,请妥善保管,不要泄露给他人。
选项 A:Python SDK¶
详细教程请前往 Python 快速入门
Python 接入仅需一行 pip 安装,API Nexus 兼容 OpenAI 官方 SDK:
from openai import OpenAI
client = OpenAI(
api_key="nxk-你的APIKey",
base_url="https://apinexus.net/v1",
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "你好"}],
)
print(response.choices[0].message.content)
选项 B:Flutter SDK¶
环境要求¶
- Flutter 3.10+ / Dart 3.0+
- iOS 13.0+ / Android 6.0+ / Windows 10+ / macOS 11+ / Linux (Desktop)
添加依赖¶
在 pubspec.yaml 中添加:
安装依赖:
配置 API Key¶
在项目根目录创建 .env 文件:
第一次 API 调用¶
将 lib/main.dart 替换为以下内容:
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: ".env");
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'API Nexus Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const ChatHomePage(),
);
}
}
class ChatHomePage extends StatefulWidget {
const ChatHomePage({super.key});
@override
State<ChatHomePage> createState() => _ChatHomePageState();
}
class _ChatHomePageState extends State<ChatHomePage> {
final TextEditingController _input = TextEditingController();
String _response = '';
bool _isLoading = false;
Future<void> _sendMessage() async {
setState(() => _isLoading = true);
try {
final response = await http.post(
Uri.parse('${dotenv.env['APINEXUS_BASE_URL']!}/chat/completions'),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ${dotenv.env['APINEXUS_API_KEY']!}',
},
body: json.encode({
'model': 'gpt-4o-mini',
'messages': [{'role': 'user', 'content': _input.text}],
}),
);
final data = json.decode(response.body);
setState(() => _response = data['choices'][0]['message']['content']);
} catch (e) {
setState(() => _response = '调用失败: $e');
} finally {
setState(() => _isLoading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('API Nexus 快速入门')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
TextField(
controller: _input,
decoration: const InputDecoration(hintText: '输入你的问题'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _isLoading ? null : _sendMessage,
child: Text(_isLoading ? '请求中...' : '发送'),
),
const SizedBox(height: 24),
Expanded(
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(8),
),
child: SingleChildScrollView(
child: Text(_response.isEmpty ? '回复将显示在这里' : _response),
),
),
),
],
),
),
);
}
}
运行:
📱 Flutter 完整教程请前往 Flutter 教程专区
下一步学习¶
Python 进阶¶
| 教程内容 | 链接 |
|---|---|
| 文本对话进阶 | 详细教程 |
| 图像生成指南 | 详细教程 |
| Embedding 使用 | 详细教程 |
| 语音合成实战 | 详细教程 |
| 多模态识图 | 详细教程 |
Flutter 进阶¶
| 教程内容 | 链接 |
|---|---|
| 文本对话 / 流式输出 | 详细教程 |
| 图像生成与预览 | 详细教程 |
| 向量计算 / 语义搜索 | 详细教程 |
| 多模态识图 / OCR | 详细教程 |
| 音频处理 / TTS | 详细教程 |
最后更新: 2026-06-19