Ba điều hữu ích khi bạn dùng công cụ dòng lệnh hằng ngày hoặc trong script. Mỗi điều cũng cho thấy cách làm tương tự trong code với SDK hoặc API.
Nhiều tài khoản: hồ sơ
apidly login --profile NAME lưu một khóa dưới một tên, bên cạnh khóa mặc định của bạn. Chọn hồ sơ cho một lệnh bằng --profile NAME hoặc biến môi trường APIDLY_PROFILE, hoặc đặt làm mặc định bằng apidly config use NAME. apidly config profiles liệt kê các hồ sơ và apidly config profiles rm NAME xóa một hồ sơ. Khi APIDLY_API_KEY được đặt, nó được ưu tiên hơn mọi hồ sơ đã lưu.
apidly login --profile work
apidly config profiles
apidly servers --profile work
APIDLY_PROFILE=work apidly balance
apidly config use work
apidly config profiles rm workCode không có hồ sơ: mỗi khóa là một client riêng. Giữ mỗi khóa trong biến môi trường riêng của nó, không bao giờ trong code.
curl "https://apidly.com/api/v1/me" \
-H "Authorization: Bearer $APIDLY_WORK_KEY"import { Apidly } from "@apidly/sdk";
const personal = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const work = new Apidly({ apiKey: process.env.APIDLY_WORK_KEY });
console.log((await work.billing.balance()).balance);
console.log((await personal.billing.balance()).balance);import os
from apidly import Client
personal = Client(api_key=os.environ["APIDLY_API_KEY"])
work = Client(api_key=os.environ["APIDLY_WORK_KEY"])
print(work.billing.balance()["balance"])
print(personal.billing.balance()["balance"])Cập nhật CLI
apidly update tìm phiên bản mới hơn, tải từ trang này, kiểm tra checksum và cài bằng npm sau khi hỏi bạn. --check chỉ báo cáo, với mã thoát 3 khi có phiên bản mới hơn, phù hợp cho một bước CI; --print in ra các lệnh thay vì chạy chúng. Phiên bản hiện tại và checksum của từng tệp cũng có trong latest.json.
apidly --version
apidly update --check
apidly update --print
apidly update --yes
curl -fsS "https://apidly.com/downloads/latest.json" | jq -r '.version'const res = await fetch("https://apidly.com/downloads/latest.json");
const latest = await res.json();
const cli = latest.files.find((f) => f.kind === "cli");
console.log(latest.version, cli.name, cli.sha256);import json
import urllib.request
with urllib.request.urlopen("https://apidly.com/downloads/latest.json") as res:
latest = json.load(res)
cli = next(f for f in latest["files"] if f["kind"] == "cli")
print(latest["version"], cli["name"], cli["sha256"])Chờ máy chủ trong script
apidly servers wait ID kết thúc với mã thoát 0 ngay khi máy chủ chạy, hoặc 1 khi nó không thể đạt trạng thái đó, chẳng hạn khi khởi chạy thất bại. --for stopped hoặc --for destroyed chờ các trạng thái đó, và --timeout bỏ cuộc sau số giây đó (mặc định 600). apidly deploy --wait và apidly stop --wait chờ ngay trong cùng lệnh. Ctrl-C kết thúc việc chờ với mã thoát 130 và để máy chủ như cũ. Với API, hãy đọc status của máy chủ vài giây một lần.
apidly servers wait ins_ID
apidly servers wait ins_ID --for stopped --timeout 300
apidly deploy --plan rtx-4090-1x --template pytorch --yes --wait
apidly stop ins_ID --wait
until [ "$(curl -fsS "https://apidly.com/api/v1/servers/ins_ID" \
-H "Authorization: Bearer $APIDLY_API_KEY" | jq -r '.server.status')" = running ]; do
sleep 5
doneimport { Apidly } from "@apidly/sdk";
const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
for (;;) {
const { server } = await apidly.servers.get("ins_ID");
if (server.status === "running") break;
if (server.status === "error" || server.status === "destroyed") {
throw new Error(server.statusMessage ?? server.status);
}
await new Promise((r) => setTimeout(r, 5000));
}import os
import time
from apidly import Client
client = Client(api_key=os.environ["APIDLY_API_KEY"])
while True:
server = client.servers.get("ins_ID")["server"]
if server["status"] == "running":
break
if server["status"] in ("error", "destroyed"):
raise SystemExit(server.get("statusMessage") or server["status"])
time.sleep(5)Sẵn sàng thử chưa?
Tạo tài khoản miễn phí và nạp tín dụng khi bạn muốn khởi chạy máy chủ. Mức nạp tối thiểu là 10 $.
Nội dung này có hữu ích không?