PostmanでSaaSus Platform APIを実行する

こんにちは、郡山です。
今回は 「SaaSの開発から運用まで」の支援を可能とするツール SaaSus Platform のAPI を Postmanで実行する方法をご紹介いたします。

SaaSus Platform についてはこちら
https://saasus.io/

Postman についてはこちら
https://www.postman.com/

SaaSus Platform API を呼び出すには Authorization ヘッダーに署名を設定する必要があります。署名は日時情報+API Key+HTTPメソッド+host+port+path+Request Body 情報を SaaSus Platform で発行しているSecret Key で HMAC-SHA256 を求めてそれを設定します。そのためPostmanのPre-Request Scriptを利用して署名を作成し Authorization ヘッダーへ追加してリクエストを行う必要があります。

まず Postman へ環境設定を取り込みます。
以下環境変数をテキスト等で保存して環境インポートしてください。

環境変数
{
	"id": "61c7ec1b-28c9-4221-969e-8bf307495aa5",
	"name": "SaaSus Platform Environments",
	"values": [
		{
			"key": "SaaSID",
			"value": "",
			"type": "default",
			"enabled": true
		},
		{
			"key": "APIKey",
			"value": "",
			"type": "default",
			"enabled": true
		},
		{
			"key": "SecretKey",
			"value": "",
			"type": "default",
			"enabled": true
		},
		{
			"key": "SecretKey_Stripe",
			"value": "",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlAuth",
			"value": "https://api.saasus.io/v1/auth",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlPricing",
			"value": "https://api.saasus.io/v1/pricing",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlStripe",
			"value": "https://api.saasus.io/v1/stripe",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlBilling",
			"value": "https://api.saasus.io/v1/billing",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlIntegration",
			"value": "https://api.saasus.io/v1/integration",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlAwsMarketplace",
			"value": "https://api.saasus.io/v1/awsmarketplace",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlCommunication",
			"value": "https://api.saasus.io/v1/communication",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlApiLogs",
			"value": "https://api.saasus.io/v1/apilog",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlAnalysis",
			"value": "https://api.saasus.io/v1/analysis",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlEventBridge",
			"value": "https://api.saasus.io/v1/integration",
			"type": "default",
			"enabled": true
		},
		{
			"key": "UrlSmartAPIGateway",
			"value": "https://api.saasus.io/v1/apigateway",
			"type": "default",
			"enabled": true
		}
	],
	"_postman_variable_scope": "environment",
	"_postman_exported_at": "2025-05-27T01:59:01.367Z",
	"_postman_exported_using": "Postman/11.47.1-250526-2336"
} 

取り込むと以下の設定となるので SaaSus Platformの開発コンソール->基本設定->APIキー画面に設定されているSaaSID、APIKey、SecretKeyを初期値へそれぞれ設定して保存してください。

続いてAPIを実行するためコレクションの設定を行なって行きます。

SaaSus Platform Auth APIをインポートします
Auth API: https://docs.saasus.io/file/saasus_api/authapi.yml
※ 他のAPIに関しては下表に記載

インポートするとコレクションが作成されますので、コレクションの設定を変更します。

認可タブで Auth Type を認証なしに設定します。

コレクションの Script->Pre-req へスクリプトを設定します。

こちらが設定するスクリプトとなります。

Pre-req
// 「yyyymmddhhmm」形式の日付時刻文字列に変換する関数
function formatDateInYyyymmddhhmi() {
    const date = new Date();

    const yyyy = date.getUTCFullYear().toString();
    const mm = ("00" + (date.getUTCMonth() + 1)).slice(-2);
    const dd = ("00" + (date.getUTCDate())).slice(-2);
    const hh = ("00" + (date.getUTCHours())).slice(-2);
    const mi = ("00" + (date.getUTCMinutes())).slice(-2);

    return yyyy + mm + dd + hh + mi;
}

// ボディに含まれるバインドパラメータを値に変更する
function replaceBodyBindParameterToValue(body){

    // newmanで実行すると undefined になるので空文字列を設定
    if (body === undefined){
        return "";
    }

    // NULLの場合そのまま
    if (!Object.keys(body).length){
        return body;
    };

    body = JSON.stringify(body.raw);
    body = pm.collectionVariables.replaceIn(body);
    body = pm.variables.replaceIn(body);
    return JSON.parse(body);
}

function getQueryString(queryString) {
    const queryStart = queryString.indexOf("?");
    if (queryStart === -1) return "";
    return queryString.slice(queryStart);
}

// 署名を作成する
function makeSecretHash(){

    // 日時情報
    const yyyymmddhhmi = formatDateInYyyymmddhhmi();

    // パスに含まれるバインドパラメータを値に変更する
    var path = pm.collectionVariables.replaceIn(pm.request.url.getPath()) 
                + getQueryString(pm.request.url.toString());
    const body = replaceBodyBindParameterToValue(pm.request.body);

    // Pre-Request Scriptでは パスが取れない(暫定)
    var BaseUrl = ""
    if (pm.request.url.host[0] === "{{UrlAuth}}"){
        BaseUrl = (pm.environment.get("UrlAuth") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlBilling}}") {
        BaseUrl = (pm.environment.get("UrlBilling") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlPricing}}") {
        BaseUrl = (pm.environment.get("UrlPricing") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlStripe}}") {
        BaseUrl = (pm.environment.get("UrlStripe") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlCommunication}}") {
        BaseUrl = (pm.environment.get("UrlCommunication") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlAwsMarketplace}}") {
        BaseUrl = (pm.environment.get("UrlAwsMarketplace") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlApiLogs}}") {
        BaseUrl = (pm.environment.get("UrlApiLogs") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlAnalysis}}") {
        BaseUrl = (pm.environment.get("UrlAnalysis") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlEventBridge}}") {
        BaseUrl = (pm.environment.get("UrlEventBridge") + path).split("//");
    } else if (pm.request.url.host[0] === "{{UrlSmartAPIGateway}}") {
        BaseUrl = (pm.environment.get("UrlSmartAPIGateway") + path).split("//");
    }
    
    // 日時情報+API Key+HTTPメソッド+URL(Host:Port/URI)+Request Body
    const message = yyyymmddhhmi + pm.environment.get("APIKey") + pm.request.method + BaseUrl[1] + body;
    const sha256 = CryptoJS.HmacSHA256(message, pm.environment.get("SecretKey"));

    // console.log(message);
    return CryptoJS.enc.Hex.stringify(sha256);
}

const Sig = makeSecretHash();
pm.request.headers.add(`SAASUSSIGV1 Sig=${Sig}, SaaSID=${pm.environment.get("SaaSID")}, APIKey=${pm.environment.get("APIKey")}`, "Authorization");

ここまでで共通の設定項目は終了です。

続いては実行するAPIの設定になります。
APIを取り込むと初期設定が {{baseUrl}}/basic-info となっているので {{UrlAuth}}/basic-info に変更します。
※ 呼び出すAPIにより設定は異なります、他のAPIに関しては下表に記載

認可タブで Auth Type を「上位の認可設定を継承」を設定します

上記設定後、送信ボタンを押下するとAPIが実行され結果が返ってくることが確認できます。

Postman を利用すると気軽にAPIを実行することができ動作検証が可能です。
また、環境定義の切り替えを行うだけで他の環境へ接続することができるので SaaSus Platform のテストで非常に役立っています。

各APIのymlはこちらのURLを参照
Auth API:https://docs.saasus.io/file/saasus_api/authapi.yml
Pricing API:https://docs.saasus.io/file/saasus_api/pricingapi.yml
Billing API:https://docs.saasus.io/file/saasus_api/billingapi.yml
Communication API:https://docs.saasus.io/file/saasus_api/communicationapi.yml
ApiLog API:https://docs.saasus.io/file/saasus_api/apilogapi.yml
Analysis API:https://docs.saasus.io/file/saasus_api/analysisapi.yml

API呼び出し時の{{baseUrl}}は以下に変更して実行
Auth API :{{baseUrl}} → {{UrlAuth}}
Pricing API :{{baseUrl}} → {{UrlPricing}}
Billing API :{{baseUrl}} → {{UrlBilling}}
Communication API :{{baseUrl}} → {{UrlCommunication}}
ApiLog API :{{baseUrl}} → {{UrlApiLogs}}
Analysis API :{{baseUrl}} → {{UrlAnalysis}}