# 외부 API 통신하기

외부 API에 GET, POST 등의 요청을 인자와 함께 보낼 수 있습니다.

### httpGet

[<mark style="color:purple;">한국어 별명 생성기</mark>](https://nickname.hwanmoo.kr/) API를 이용해 입장하는 플레이어의 닉네임을 바꿔보기

![](https://2461137890-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiW553XSGeKCAPpImxXi3%2Fuploads%2F4NxVaReOQTRYUh5Hba16%2FUntitled.png?alt=media\&token=7e3f7d3e-8df9-4d2b-b184-6443f4c9ba27)

```jsx
// 플레이어가 입장할 때 동작하는 함수
App.onJoinPlayer.Add(function (player) {
	App.httpGet(
		"https://nickname.hwanmoo.kr/?format=json&count=1&max_length=6&whitespace=_",
		null,
		function (res) {
			// 응답 결과를 JSON 오브젝트로 변경
			let response = JSON.parse(res);
			player.name = response.words[0];
			player.sendUpdated();
		}
	);
});
```

### httpPost

앱에서 보낸 헤더와 데이터를 응답으로 받아 채팅창에 출력해보기.

```jsx
// q를 눌렀을 때 실행되는 함수
App.addOnKeyDown(81, function (player) {
	App.httpPost(
		"https://postman-echo.com/post",
		{
			"test-header": "zep",
		},
		{
			name: "zepscript",
		},
		(res) => {
			let response = JSON.parse(res);
			App.sayToAll(`보낸 헤더: ${response.headers["test-header"]}`, 0xffffff);
			App.sayToAll(`보낸 데이터: ${response.form.name}`, 0xffffff);
		}
	);
});
```

{% hint style="warning" %}
참고&#x20;

\- 튜토리얼의 App type은 미니게임(Mini game)을 권장합니다.&#x20;

\- .js파일의 파일이름은 반드시 main 이어야 합니다. main.js 파일을 준비합니다.&#x20;

\- 배포 방법을 아직 모르신다면, [<mark style="color:purple;">ZEP Script 배포 가이드</mark>](https://docs-kr.zep.us/creator/dev-guide/zep-script)를 참고해주세요.
{% endhint %}
