# PHP Laravel & Node.js Express — API Store Hub

> VI: Gọi AI API từ Laravel `Http::withToken()` / Queue Job và Express TypeScript qua https://apistorehub.com/v1, tối ưu timeout PHP-FPM và stream SSE không nghẽn worker.
> EN: Integrate https://apistorehub.com/v1 into Laravel HTTP client jobs and Node.js Express APIs without blocking PHP-FPM workers or dropping SSE streams.

- Canonical VI: https://apistorehub.com/vi/tich-hop/php-laravel-nodejs-express
- Canonical EN: https://apistorehub.com/en/tich-hop/php-laravel-nodejs-express
- Language: PHP / TypeScript
- Runtime: PHP 8.2+ (Laravel 11) / Node.js 20+ (Express 5)
- Protocol: OpenAI Compatible (/v1/chat/completions)
- Install: `npm install openai express`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Store `AI_BASE_URL=https://apistorehub.com/v1` and `API_KEY=sk-ash-...` in server environment variables.
2. In Laravel, call `Http::withToken(...)->timeout(90)->post(...)` inside queued jobs for long tasks.
3. In Express, configure `new OpenAI({ baseURL: "https://apistorehub.com/v1" })` with `text/event-stream` headers.
4. Wire client disconnect signals to `AbortController` so abandoned requests stop immediately.

## Production Code Sample
```
import express from "express";
import OpenAI from "openai";

const app = express();
app.use(express.json());
const openai = new OpenAI({ baseURL: "https://apistorehub.com/v1", apiKey: process.env.API_KEY });

app.post("/chat", async (req, res) => {
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  const stream = await openai.chat.completions.create({
    model: "gpt-6.1-terra",
    stream: true,
    messages: [{ role: "user", content: String(req.body.prompt || "Hello") }],
  });
  for await (const chunk of stream) {
    const text = chunk.choices[0]?.delta?.content || "";
    if (text) res.write(`data: ${JSON.stringify({ text })}\n\n`);
  }
  res.end();
});
```

## Quick cURL Check
```bash
curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-6.1-terra","messages":[{"role":"user","content":"Generate product meta description"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `gpt-6.1-terra` | 3,400 ST / 1M tokens | 1.1M | https://apistorehub.com/llms/models/gpt-6-1-terra.md |
| `deepseek-v4-flash` | 1,500 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-flash.md |
| `req/gpt-image-2.5` | 2,500 ST / req | - | https://apistorehub.com/llms/models/gpt-image-2-5.md |

## FAQ
- **Does `openai-php/laravel` work out of the box with API Store Hub?**: Yes. Set `OPENAI_BASE_URI=https://apistorehub.com/v1` and `OPENAI_API_KEY` to your `sk-ash-` key.
- **How should Laravel apps handle batch content generation?**: Dispatch queued jobs targeting `deepseek-v4-flash` and persist outputs to avoid duplicate generation.
- **Can one Express OpenAI client call both chat and image endpoints?**: Yes. Both `chat.completions.create` and `images.generate` share the same `/v1` base URL.
