---
title: $input.first() – ดึง item แรกจาก input ของ node ปัจจุบัน
url: https://www.thepexcel.com/functions/n8n/other/input-first-n8n/
type: function-explainer
program: n8n
syntax: $input.first()
date: 2025-12-16
updated: 2025-12-22
scores:
  popularity: 8
  difficulty: 2
  usefulness: 8
---

# $input.first() – ดึง item แรกจาก input ของ node ปัจจุบัน

> $input.first() ดึงเฉพาะ item แรกสุดจากข้อมูลที่เข้ามาใน node ปัจจุบัน ใช้เมื่อต้องการ configuration,

## คำอธิบาย

$input.first() ดึงเฉพาะ item แรกสุดจากข้อมูลที่เข้ามาใน node ปัจจุบัน ใช้เมื่อต้องการ configuration, header row หรือข้อมูลอ้างอิงที่อยู่ตำแหน่งแรกเสมอ return เป็น item object เดียว (ไม่ใช่ array) ถ้าไม่มี items จะได้ undefined

## Syntax

```excel
$input.first()
```

## เคสการใช้งาน

### ดึง configuration หรือ settings จาก item แรกก่อน p...

ดึง configuration หรือ settings จาก item แรกก่อน process items อื่น

_เหมาะกับ:_ general

### Extract header row จาก CSV/Excel ที่แปลงมาเป็น JSO...

Extract header row จาก CSV/Excel ที่แปลงมาเป็น JSON

_เหมาะกับ:_ general

### ดึง metadata หรือ summary information ที่อยู่ item...

ดึง metadata หรือ summary information ที่อยู่ item แรกเสมอ

_เหมาะกับ:_ general

### Validation ว่ามีข้อมูลเข้ามาหรือไม่โดยเช็ค item แร...

Validation ว่ามีข้อมูลเข้ามาหรือไม่โดยเช็ค item แรก

_เหมาะกับ:_ general

## ตัวอย่าง

### 1. ดึง config จาก item แรก

```excel
{{ $input.first().json.apiKey }}
```

**ผลลัพธ์:** `sk-xxxx1234`

สมมติ input มี item แรกที่เก็บ config เช่น {apiKey: 'sk-xxxx1234', timeout: 5000} ใช้ $input.first().json.apiKey ดึงค่า API Key ออกมาใช้งานได้ทันที pattern นี้ใช้บ่อยมากเมื่อมี Set node ส่ง config มาก่อนครับ

### 2. ดึง header row จากข้อมูล CSV

```excel
{{ $input.first().json.headers.join(', ') }}
```

**ผลลัพธ์:** `Name, Email, Phone`

เมื่อ import CSV แล้วแถวแรกเป็น header ใช้ $input.first() ดึง header ออกมาได้ทันที ไม่ต้องใช้ $input.all()[0] ให้ยุ่งยาก ส่วนตัวผมใช้ pattern นี้บ่อยมากตอนทำ data transformation

### 3. ใช้ร่วมกับ optional chaining ป้องกัน error

```excel
{{ $input.first()?.json?.email ?? 'no-email@example.com' }}
```

**ผลลัพธ์:** `user@example.com หรือ no-email@example.com`

ใช้ ?. (optional chaining) ป้องกัน error ถ้าไม่มี items หรือไม่มี field email และใช้ ?? ให้ค่า default ถ้าได้ undefined นี่คือ best practice ที่ควรใช้ใน production workflow ครับ

### 4. เช็คว่ามี input หรือไม่ก่อน process

```excel
{{ $input.first() ? 'Ready to process' : 'No data' }}
```

**ผลลัพธ์:** `Ready to process`

ใช้ ternary operator เช็คว่ามี item แรกหรือไม่ ถ้าไม่มีจะได้ undefined ซึ่งเป็น falsy value ใช้ใน IF node หรือ Switch node สำหรับ validation และ error handling ได้ดีครับ

## หมายเหตุเพิ่มเติม

- ใช้ดึง configuration หรือ settings จาก setup node ที่ส่งมาก่อน data nodes

- ใช้ optional chaining (?.) เสมอเพื่อป้องกัน error ถ้าไม่มี items

- ใช้ใน IF node เช็คว่ามี data เข้ามาหรือไม่ก่อน process ต่อ

## คำถามที่พบบ่อย

**Q: ถ้าไม่มี items เข้ามาเลยจะเกิดอะไรขึ้น?**

จะได้ undefined กลับมาครับ ถ้าพยายามเข้าถึง .json ต่อจะเกิด error ทันที ดังนั้นควรใช้ optional chaining ($input.first()?.json) หรือเช็คด้วย ternary operator ก่อนเข้าถึงข้อมูลเสมอนะครับ 😅

**Q: $input.first() ต่างจาก $input.all()[0] ยังไง?**

ผลลัพธ์เหมือนกันครับ แต่ $input.first() อ่านชัดเจนกว่าว่าเราต้องการ item แรก และ handle edge case ดีกว่า ถ้าไม่มี items จะได้ undefined ส่วน $input.all()[0] จะได้ undefined เหมือนกัน แต่โค้ดยาวกว่า

**Q: ใช้ใน Python Code node ได้ไหม?**

ได้ครับ แต่ใน Python ใช้ _input.first แทน (underscore แทน $) และเป็น property ไม่ใช่ method ดังนั้นไม่ต้องใส่วงเล็บ เช่น first_item = _input.first

## แหล่งข้อมูลเพิ่มเติม

- [n8n Current Node Input Documentation](https://docs.n8n.io/code/builtin/current-node-input/) _(article)_
- [n8n Expressions Cheat-Sheet](https://logicworkflow.com/blog/n8n-expressions/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/n8n/other/input-first-n8n/](https://www.thepexcel.com/functions/n8n/other/input-first-n8n/)_
