开发者

How to create API

I'm finding a lot of tutorials on how to implement APIs but how does one actually create one? For example, I want to query movie quotes from my DB and also insert new ones via an API. I know PH开发者_如何转开发P and MySQL but what am I missing to make an API? I read about the REST method and it seems easy but I can't find a step-by-step guide.

Can someone please share an example or some steps to creating a simple API?


Worth doing Sarfraz's article to understand design and then the mechanics/plumbing. Once you understand what your API looks like, definitely go back an evaluate REST frameworks available for PHP or whatever language you're working with. Friends don't let friends right their own rest plumbing in 2010.

Beyond just making it send and receive xml/json/etc., the idea of what makes a good API is something many have considered--worth reading up on imo. People who are smarter than me have said that great APIs that are designed with the way developers user them in mind, first and foremost... from there they work backwards into a black box their consumers shouldn't have to care about.


You may be interested in:

  • Creating a REST API with PHP


If you are not restricted to implementing the API in PHP, here is a quick setup guide - for NodeJS based REST APIs. This talks about implementing a simple ping API in NodeJS, building and releasing it to production.

Prerequisites

  • AWS Account: Sign up on AWS. You will get a Free Tier of 12 months – which means you get first million requests to your API every month for free.
  • IAM User on AWS: Once signed up, go to IAM on AWS Console create a new IAM user for yourself. Go to the Security Credentials of the new user, create an Access Key and Secret Key for this user and download it, keep it somewhere safe.
  • Install AWS CLI.
  • Install AWS SAM. Detailed instructions here.

Setup your codebase

Set some local environment variables to use later

export AWS_REST_SERVICE_NAME=MyRestService
export AWS_REST_SERVICE_S3_BUCKET_NAME=MyRestService # This has to be unique globally

Clone my sample repository. This has a simple ping GET Api written in NodeJS 8.1

git clone https://github.com/sarthakj178/AWSRestServiceTemplate.git $AWS_REST_SERVICE_NAME
cd $AWS_REST_SERVICE_NAME
npm install

That’s it. Your REST Service is setup locally. Test it out.

sam local start-api # This will start your service locally on port 3000
curl -X GET "http://localhost:3000/ping" # On a different tab. Should return {"success":true,"message":"Hello world"}

Release to production

aws s3 mb s3://$AWS_REST_SERVICE_S3_BUCKET_NAME
sam package --template-file template.yaml   --s3-bucket $AWS_REST_SERVICE_S3_BUCKET_NAME --output-template-file packaged.yaml
sam deploy --template-file ./packaged.yaml --stack-name $AWS_REST_SERVICE_NAME --capabilities CAPABILITY_IAM

Verify your service in production

  • Go to AWS Console -> API Gateway -> YOUR_REST_SERVICE_NAME -> Stages -> Prod -> ping -> GET

  • You will see a URL similar to https://xxxx.execute-api.rr-rrrrr-r.amazonaws.com/Prod/ping.

  • This is your REST Service in production. Hit it, you’ll see a response same as what you saw on your local desktop.


You can create sample api here sample api

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜