Getting started
Learn how to quickly get started with Trainfog API
Why Connect to the Trainfog API? 🔌
The Trainfog API provides a powerful, programmatic interface to manage your product catalog, content generation workflows, and e-commerce assets. Connecting to the API allows you to seamlessly integrate Trainfog's advanced AI and data management features directly into your existing applications, platforms, and third-party services.
This integration is essential for:
- Content Automation: Automatically generate rich product descriptions, SEO metadata, and marketing copy using cutting-edge AI models, linking the output directly to your product catalog.
- Data Synchronization: Keep your products, brands, and categories synchronized across multiple storefronts, PIMs (Product Information Management systems), or internal tools.
- Custom Workflows: Build specialized applications that leverage Trainfog's backend logic, such as triggering content generation upon a new product import or updating SEO fields based on AI analysis.
**Authentication: OAuth 2.0 Authorization Code Grant Flow **
Access to the Trainfog API is secured using the OAuth 2.0 Authorization Code Grant flow. This standard protocol is mandatory for secure, delegated access, ensuring that client applications can access user data without ever handling the user's password.
Stage 1: Application Setup and Authorization Request
1. Register Your Application
Before starting the flow, you must register your application to obtain the necessary credentials:
- Log in to your Trainfog account dashboard.
- Navigate to the Developer panel.
- Create a new application to get your Client ID and a Client Secret.
- Configure the official Redirect URIs where Trainfog will send the authorization code.
2. Request Authorization Code (GET /oauth/authorize)
Your application initiates the flow by redirecting the user's browser to the Trainfog authorization endpoint. The user is prompted to log in and grant permissions.
| Parameter | Location | Description |
|---|---|---|
| client_id | Query | Your unique application identifier (obtained from the Developer Panel). |
| redirect_uri | Query | The registered URL where the user is sent after authorization. |
| response_type | Query | Must be code. |
| scope | Query | A space-delimited list of requested permissions (e.g., products:read). |
| state | Query | Crucial unique string for CSRF protection. |
3. Handle User Consent (POST /oauth/authorize)
After the user approves your application in the Trainfog UI, the backend processes the consent and redirects the user back to your redirect_uri.
Successful Redirect:
YOUR_REDIRECT_URI?code=AUTHORIZATION_CODE&state=CSRF_STATE
The returned AUTHORIZATION_CODE is single-use and valid for a very short period.
Stage 2: Token Exchange
This stage involves your secure backend server exchanging the temporary code for permanent access tokens.
4. Exchange Code for Access Token (POST /oauth/token)
Your application makes a secure, server-to-server request to the token endpoint.
| Parameter | Value | Description |
|---|---|---|
| grant_type | authorization_code | Specifies the flow type. |
| client_id | Your ID | Authenticates your application. |
| client_secret | Your Secret | Validates the request security. |
| code | AUTHORIZATION_CODE | The code received from Stage 1. |
| redirect_uri | Your URI | Must exactly match the original URI. |
Success Response (Contains Tokens):
{
"access_token": "ACCESS_TOKEN_VALUE",
"refresh_token": "REFRESH_TOKEN_VALUE",
"expires_in": 3600, // 1 hour
"token_type": "Bearer",
"workspace": "USER_WORKSPACE_ID",
"scope": "list of granted scopes"
}Stage 3: API Access and Token Refresh
5. Accessing the Trainfog API
You must use the access_token in the Authorization header for all requests:
Authorization: Bearer YOUR_ACCESS_TOKEN
Important Notes:
- The access_token is valid for 1 hour (3600 seconds).
- The Trainfog API automatically inherits the necessary Workspace ID and user context from this token, removing the need to specify it in query parameters or request bodies.
6. Managing Token Expiration and Refresh
To ensure continuous service, you must use the refresh_token to acquire a new token pair before the current access_token expires or when you receive a 401 Unauthorized error.
Requesting a Refresh (POST /oauth/token):
| Parameter | Value | Description |
|---|---|---|
| grant_type | refresh_token | Specifies the refresh operation. |
| refresh_token | REFRESH_TOKEN_VALUE | The long-lived token obtained in Stage 2. |
Success Response: Returns a brand new access_token and a new refresh_token (which you should save to replace the old one), renewing the 1-hour window.
Your Quickstart Path
For detailed information about available endpoints, request/response formats, and usage examples, please refer to our comprehensive API Reference documentation.
Register & Configure Application
Register your Trainfog Account and create your application in the Developer panel to obtain your Client ID and Client Secret.
Obtain Authorization Code
Redirect the user to the GET /oauth/authorize\ endpoint to initiate user consent and receive the one-time authorization code via your registered redirect_uri.
Exchange Code for Tokens
Make a secure, server-side POST request to /oauth/token using the authorization code and your client_secret to obtain the initial access_token and refresh_token.
Make your first API call
Use the access_token in the Authorization: Bearer header to access any resource, like checking user info.
curl -X GET \
https://api.trainfog.com/oauth/userinfo \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'Implement Token Refresh Logic
Before the token expires (or after a 401 error), use the refresh_token in a POST /oauth/token request to acquire a new token pair and ensure continuous service.