A progressive Node.js framework for building efficient and scalable server-side applications.
Nest framework TypeScript starter repository.
$ pnpm installCreate a .env file in the root of the api directory with the following variables:
# OAuth 1.0a - Consumer Keys (for posting tweets)
TWITTER_API_KEY=your_consumer_api_key
TWITTER_API_SECRET=your_consumer_api_secret
TWITTER_ACCESS_TOKEN=your_access_token
TWITTER_ACCESS_TOKEN_SECRET=your_access_token_secretTWITTER_CLIENT_ID=your_oauth2_client_id
TWITTER_CLIENT_SECRET=your_oauth2_client_secret
TWITTER_REDIRECT_URI=your_redirect_uriHow to get these credentials:
- Go to the Twitter Developer Portal
- Create or select your app
- IMPORTANT: Set app permissions to "Read and Write" in Settings → User authentication settings
- In the app settings, you'll find:
- API Key (Consumer Key) →
TWITTER_API_KEY - API Secret (Consumer Secret) →
TWITTER_API_SECRET
- API Key (Consumer Key) →
- Under "Keys and tokens", generate (or regenerate if permissions changed):
- Access Token →
TWITTER_ACCESS_TOKEN - Access Token Secret →
TWITTER_ACCESS_TOKEN_SECRET
- Access Token →
- 401 Unauthorized Error:
- Your app must have "Read and Write" permissions
- If you changed permissions, you MUST regenerate the Access Token and Access Token Secret
- Make sure all 4 credentials (API Key, API Secret, Access Token, Access Token Secret) are correctly copied to your
.envfile - Ensure there are no extra spaces or quotes in your
.envfile
Note: The TWITTER_CLIENT_ID and TWITTER_CLIENT_SECRET are for OAuth 2.0 user authentication (different from posting tweets). For posting, you need OAuth 1.0a credentials: TWITTER_API_KEY, TWITTER_API_SECRET, TWITTER_ACCESS_TOKEN, and TWITTER_ACCESS_TOKEN_SECRET.
# development
$ pnpm run start
# watch mode
$ pnpm run start:dev
# production mode
$ pnpm run start:prod# unit tests
$ pnpm run test
# e2e tests
$ pnpm run test:e2e
# test coverage
$ pnpm run test:covThe Marketplace Streaming API provides real-time Server-Sent Events (SSE) streaming for AI chat responses. This endpoint is designed specifically for marketplace integrations and operates independently of the main application's credit system.
POST /ai/marketplace-stream
Required Header:
x-marketplace-key: YOUR_MARKETPLACE_API_KEY
The marketplace API key should be set in your .env file:
MARKETPLACE_API_KEY=your_secure_marketplace_key{
"messages": [
{
"id": "unique-message-id",
"chatId": "unique-chat-id",
"role": "user",
"content": [
{
"type": "text",
"text": "Your question here"
}
],
"createdAt": "2024-01-15T10:30:00.000Z"
}
],
"chatId": "unique-chat-id"
}The endpoint returns Server-Sent Events (SSE) with the following format:
data: {"token":"Hello"}
data: {"token":" there"}
data: {"token":"!"}
Each event contains a token field with a chunk of the AI response.
- ✅ Real-time token streaming
- ✅ Conversation history support
- ✅ No credit system (free for marketplace)
- ✅ Same educational AI tutor as main app
- ✅ Messages saved to database
- ✅ Marketplace-only authentication
const response = await fetch('http://your-api-url/ai/marketplace-stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-marketplace-key': 'YOUR_MARKETPLACE_KEY'
},
body: JSON.stringify({
messages: [
{
id: 'msg-1',
chatId: 'chat-123',
role: 'user',
content: [{ type: 'text', text: 'Explain Solana DeFi' }],
createdAt: new Date().toISOString()
}
],
chatId: 'chat-123'
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
console.log(data.token); // Process each token
}
}
}import requests
import json
headers = {
'Content-Type': 'application/json',
'x-marketplace-key': 'YOUR_MARKETPLACE_KEY'
}
payload = {
'messages': [
{
'id': 'msg-1',
'chatId': 'chat-123',
'role': 'user',
'content': [{'type': 'text', 'text': 'Explain Solana DeFi'}],
'createdAt': '2024-01-15T10:30:00.000Z'
}
],
'chatId': 'chat-123'
}
response = requests.post(
'http://your-api-url/ai/marketplace-stream',
headers=headers,
json=payload,
stream=True
)
for line in response.iter_lines():
if line.startswith(b'data: '):
data = json.loads(line[6:])
print(data['token'], end='', flush=True)A test HTML file is included at api/test-marketplace-stream.html for manual testing. To use it:
- Open the file in a browser
- Enter your API URL (e.g.,
http://localhost:3000) - Enter your marketplace API key
- Enter a message
- Click "Start Stream" to test the connection
| Status Code | Description |
|---|---|
| 200 | Success - SSE stream initiated |
| 400 | Bad request - Invalid message format |
| 401 | Unauthorized - Invalid or missing marketplace API key |
| 404 | Not found - Marketplace user not configured |
| 500 | Internal server error |
The endpoint supports conversation history by including previous messages in the messages array:
{
"messages": [
{
"id": "1",
"chatId": "chat-123",
"role": "user",
"content": [{"type": "text", "text": "What is Solana?"}],
"createdAt": "2024-01-15T10:30:00.000Z"
},
{
"id": "2",
"chatId": "chat-123",
"role": "assistant",
"content": [{"type": "text", "text": "Solana is a high-performance blockchain..."}],
"createdAt": "2024-01-15T10:30:15.000Z"
},
{
"id": "3",
"chatId": "chat-123",
"role": "user",
"content": [{"type": "text", "text": "Tell me about PDAs"}],
"createdAt": "2024-01-15T10:31:00.000Z"
}
],
"chatId": "chat-123"
}- Marketplace API Key: Set
MARKETPLACE_API_KEYin your.envfile - Marketplace User: Ensure a user with email
marketplace@edulearn.comexists in your database- You can create this user using the script in
api/scripts/create-marketplace-user.ts
- You can create this user using the script in
- This endpoint does NOT consume user credits
- This endpoint does NOT require JWT authentication
- This endpoint uses the same AI model and system prompt as the main app (without user personalization)
- All messages are saved to the database
- The AI responses are powered by Gemini 2.5 Flash
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.
If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
$ pnpm install -g @nestjs/mau
$ mau deployWith Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
Check out a few resources that may come in handy when working with NestJS:
- Visit the NestJS Documentation to learn more about the framework.
- For questions and support, please visit our Discord channel.
- To dive deeper and get more hands-on experience, check out our official video courses.
- Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
- Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
- Need help with your project (part-time to full-time)? Check out our official enterprise support.
- To stay in the loop and get updates, follow us on X and LinkedIn.
- Looking for a job, or have a job to offer? Check out our official Jobs board.
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
Nest is MIT licensed.