Need to combine URL paths safely without worrying about double slashes or path traversal? This guide covers everything about path joining via API, including common pitfalls, security considerations, and implementation examples.
Why Use Path Joining?
Concatenating URL paths with simple string operations often leads to bugs. Consider joining "/api/v1/" and "/users" - naive concatenation gives "/api/v1//users" with a double slash.
A proper path joining function handles these edge cases automatically.
Common Issues
Double Slashes
When both segments have slashes at the join point: /path/ + /segment = /path//segment
Missing Slashes
When neither segment has a slash: path + segment = pathsegment
Path Traversal
Malicious input like "../../../etc/passwd" can escape intended directories.
Using the Path Joining API
TinyFn provides a safe endpoint for path joining:
POST https://api.tinyfn.io/v1/url/join-path
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"base": "https://api.example.com/v1/",
"segments": ["users", "123", "profile"]
}
{
"result": "https://api.example.com/v1/users/123/profile",
"path_only": "/v1/users/123/profile",
"segments_joined": 3
}
Parameters
| Parameter | Type | Description |
|---|---|---|
base |
string | Base URL or path (required) |
segments |
array | Path segments to join (required) |
trailing_slash |
boolean | Add trailing slash to result (default: false) |
sanitize |
boolean | Remove path traversal attempts (default: true) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/url/join-path',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
base: 'https://api.example.com/v1/',
segments: ['users', '123', 'profile']
})
}
);
const { result } = await response.json();
console.log(result); // "https://api.example.com/v1/users/123/profile"
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/url/join-path',
headers={'X-API-Key': 'your-api-key'},
json={
'base': 'https://api.example.com/v1/',
'segments': ['users', '123', 'profile']
}
)
result = response.json()['result']
print(result) # "https://api.example.com/v1/users/123/profile"
cURL
curl -X POST "https://api.tinyfn.io/v1/url/join-path" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"base": "https://api.example.com/v1/", "segments": ["users", "123"]}'
Common Use Cases
- API Client Libraries: Build endpoint URLs dynamically
- File Uploads: Construct storage paths from user input safely
- URL Generation: Create links with variable path components
- Configuration: Combine base URLs with configurable paths
- Routing: Build routes from path segments
Best Practices
- Always sanitize: Keep sanitization enabled for user input
- Validate segments: Ensure segments don't contain unexpected characters
- Use arrays: Pass segments as array, not pre-concatenated strings
- URL encode: Encode special characters in path segments
Try the Path Joining API
Get your free API key and start joining paths safely in seconds.
Get Free API Key