javascript 寫入 Ragic

Ragic API Key

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

code

const axios = require('axios');

const RAGIC_API_BASE_URL = 'https://www.ragic.com/your_account_name/api/your_sheet_id';
const RAGIC_API_KEY = process.env.RAGIC_API_KEY; // 確保將 Ragic API Key 設為環境變數

/**
 * 將 CSV 資料寫入 Ragic 表單
 * @param {Array} data - 轉換後的 CSV 資料
 * @returns {Promise} - 回傳寫入結果
 */
const writeToRagic = async (data) => {
    try {
        const response = await axios.post(
            `${RAGIC_API_BASE_URL}`, // Ragic API 的目標 URL
            data, // 直接發送 JSON 格式的資料
            {
                headers: {
                    Authorization: `Basic ${Buffer.from(`${RAGIC_API_KEY}:`).toString('base64')}`, // 使用 Basic Auth 發送 API Key
                    'Content-Type': 'application/json'
                }
            }
        );

        console.log('寫入 Ragic 成功:', response.data);
        return response.data;
    } catch (error) {
        console.error('寫入 Ragic 時發生錯誤:', error.response ? error.response.data : error.message);
        throw error;
    }
};

module.exports = writeToRagic;

.env 部署在 Cloud Run

在部署到 Cloud Run 時,由於 .env 文件不會自動上傳,你需要手動設定環境變數。可以使用 gcloud 指令來設置:

gcloud run deploy your-cloud-run-service \
  --set-env-vars RAGIC_API_KEY=your_actual_ragic_api_key_here,RAGIC_API_BASE_URL=https://www.ragic.com/your_account_name/api/your_sheet_id \
  --region your-region \
  --source .