> For the complete documentation index, see [llms.txt](https://docs-epc.gitbook.io/ncs-documents/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-epc.gitbook.io/ncs-documents/public-api/getting-start-with-ncs-public-api/using-rest-api.md).

# Using REST API

## Prerequisite

* มี account ใช้งานบน NIPA Cloud Space
* มี access เข้า project ที่มี wallet status เป็น Normal

## Instructions

### 1. ดูข้อมูล project ID บน NIPA Cloud Space

เข้าสู่หน้า project detail จากการเลือก project ที่ต้องการที่หน้า projects management และเลือก copy project ID

<figure><img src="/files/u6GQ6cEtDPcf3zjhFa29" alt=""><figcaption></figcaption></figure>

### 2. Authen เข้าใช้งาน OpenStack API

ใช้ cURL ในการ authenticate OpenStack API

<pre><code>curl -i -X POST \
<strong>https://identity-api.nipa.cloud/v3/auth/tokens \
</strong>  -H "Content-Type: application/json" \
  -d '
{
  "auth": {
    "identity": {
      "methods": [
        "password"
      ],
      "password": {
        "user": {
          "name": "&#x3C;email สำหรับใช้ login เข้า NCS>",
          "password": "&#x3C;password สำหรับใช้ login เข้า NCS>",
          "domain": { "name": "nipacloud" }
        }
      }
    },
    "scope": {
      "project": {
        "id": "&#x3C;project ID ที่ copy มาจากหน้า project detail>"
      }
    }
  }
}'
</code></pre>

เมื่อได้ผลเป็น Created (201) ให้ทำการ copy X-Subject-Token ใน response header ไปใช้งานต่อ

และสามารถดู API endpoint ได้จาก catalog ใน response body

อ่านเพิ่มเติม <https://docs.openstack.org/api-quick-start/api-quick-start.html#authenticate>

***

## ตัวอย่างการใช้งาน API

ดูรายการ API สำหรับจัดการ Compute Instance ได้จาก <https://docs.openstack.org/api-ref/compute/>

### 1. ทำการ set variable

ทำการ export token variable โดยใช้ command

```
export OS_TOKEN=<X-Subject-Token ที่ได้จาก header ของการ authen>
```

export Compute API endpoint โดยใช้ command

```
export OS_COMPUTE_API=<Compute API endpoint จาก catalog ของการ authen>
```

### 2. ทำ action กับ Compute Instance

สั่ง REST API เพื่อทำ action กับ Compute Instance (Server)

```
curl -L -X POST -H "X-Auth-Token: $OS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"os-stop": null}' \
     $OS_COMPUTE_API/servers/{server_id}/action
```

***

## Reference

* keystone api ref : <https://docs.openstack.org/api-ref/identity/v3/>
* glace api ref : <https://docs.openstack.org/api-ref/image/v2/>
* nova api ref : <https://docs.openstack.org/api-ref/compute/>
* cinder api ref : <https://docs.openstack.org/api-ref/block-storage/v3/>
